Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 63d7d0f1a4222fe362c9f105fe8addcd7a4d8b23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/***************************************************************************
 * Copyright (c) 2004 - 2009 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.util.cache;

import org.eclipse.net4j.internal.util.bundle.OM;
import org.eclipse.net4j.util.ImplementationError;
import org.eclipse.net4j.util.concurrent.Worker;
import org.eclipse.net4j.util.event.Event;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public class CacheMonitor extends Worker implements ICacheMonitor
{
  // percentFreeAllocated = Round((freeMemory / totalMemory) * 100);
  // percentAllocated = Round((totalMemory / maxMemory ) * 100);

  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, CacheMonitor.class);

  private static final long DEFAULT_PAUSE_GREEN = 60L * 1000L; // 1 minute

  private static final long DEFAULT_PAUSE_YELLOW = 5L * 1000L; // 5 seconds

  private static final long DEFAULT_PAUSE_RED = 100L; // 100 milliseconds

  private long pauseGREEN = DEFAULT_PAUSE_GREEN;

  private long pauseYELLOW = DEFAULT_PAUSE_YELLOW;

  private long pauseRED = DEFAULT_PAUSE_RED;

  private ConditionPolicy conditionPolicy;

  private Condition condition;

  private Map<ICache, ICacheRegistration> registrations = new HashMap<ICache, ICacheRegistration>();

  public CacheMonitor()
  {
  }

  public long getPauseGREEN()
  {
    return pauseGREEN;
  }

  public void setPauseGREEN(long pauseGREEN)
  {
    this.pauseGREEN = pauseGREEN;
  }

  public long getPauseYELLOW()
  {
    return pauseYELLOW;
  }

  public void setPauseYELLOW(long pauseYELLOW)
  {
    this.pauseYELLOW = pauseYELLOW;
  }

  public long getPauseRED()
  {
    return pauseRED;
  }

  public void setPauseRED(long pauseRED)
  {
    this.pauseRED = pauseRED;
  }

  public ConditionPolicy getConditionPolicy()
  {
    return conditionPolicy;
  }

  public void setConditionPolicy(ConditionPolicy conditionPolicy)
  {
    this.conditionPolicy = conditionPolicy;
  }

  public Condition getCondition()
  {
    return condition;
  }

  public ICacheRegistration[] getRegistrations()
  {
    synchronized (registrations)
    {
      return registrations.values().toArray(new ICacheRegistration[registrations.size()]);
    }
  }

  public ICacheRegistration registerCache(ICache cache)
  {
    if (TRACER.isEnabled())
    {
      TRACER.trace("Registering cache " + cache);
    }
    ICacheRegistration registration = new CacheRegistration(this, cache);
    ICacheRegistration oldRegistration;
    synchronized (registrations)
    {
      oldRegistration = registrations.put(cache, registration);
    }

    if (oldRegistration != null)
    {
      oldRegistration.dispose();
    }

    return registration;
  }

  public void deregisterCache(ICache cache)
  {
    ICacheRegistration registration;
    synchronized (registrations)
    {
      registration = registrations.remove(cache);
    }

    if (registration != null)
    {
      registration.dispose();
      if (TRACER.isEnabled())
      {
        TRACER.trace("Deregistered cache " + cache);
      }
    }
  }

  @Override
  protected void doBeforeActivate() throws Exception
  {
    super.doBeforeActivate();
    if (conditionPolicy == null)
    {
      throw new IllegalStateException("conditionPolicy == null");
    }
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    for (ICacheRegistration registration : getRegistrations())
    {
      registration.dispose();
    }

    registrations.clear();
    super.doDeactivate();
  }

  @Override
  protected void work(WorkContext context) throws Exception
  {
    Condition newCondition = conditionPolicy.getNewCondition(condition);
    setCondition(newCondition);

    switch (newCondition)
    {
    case GREEN:
      context.nextWork(pauseGREEN);
      break;

    case YELLOW:
      context.nextWork(pauseYELLOW);
      break;

    case RED:
      handleConditionRED();
      context.nextWork(pauseRED);
      break;
    }
  }

  protected void setCondition(Condition newCondition)
  {
    if (newCondition == null)
    {
      throw new ImplementationError("newCondition == null");
    }

    Condition oldCondition = condition;
    if (newCondition != oldCondition)
    {
      condition = newCondition;
      fireEvent(new CacheMonitorEvent(oldCondition, newCondition));
    }
  }

  protected void handleConditionRED()
  {
    System.err.println("CONDITION RED");
  }

  /**
   * @author Eike Stepper
   */
  private final class CacheMonitorEvent extends Event implements ICacheMonitorEvent
  {
    private static final long serialVersionUID = 1L;

    private Condition oldCondition;

    private Condition newCondition;

    public CacheMonitorEvent(Condition oldCondition, Condition newCondition)
    {
      super(CacheMonitor.this);
      this.oldCondition = oldCondition;
      this.newCondition = newCondition;
    }

    public ICacheMonitor getCacheMonitor()
    {
      return CacheMonitor.this;
    }

    public Condition getOldCondition()
    {
      return oldCondition;
    }

    public Condition getNewCondition()
    {
      return newCondition;
    }
  }
}

Back to the top