1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.worker; |
20 | |
|
21 | |
import org.apache.giraph.conf.BooleanConfOption; |
22 | |
import org.apache.giraph.conf.FloatConfOption; |
23 | |
import org.apache.giraph.conf.GiraphConfiguration; |
24 | |
import org.apache.giraph.conf.IntConfOption; |
25 | |
import org.apache.giraph.utils.MemoryUtils; |
26 | |
import org.apache.giraph.utils.ThreadUtils; |
27 | |
import org.apache.giraph.zk.ZooKeeperExt; |
28 | |
import org.apache.log4j.Logger; |
29 | |
import org.apache.zookeeper.CreateMode; |
30 | |
import org.apache.zookeeper.KeeperException; |
31 | |
import org.apache.zookeeper.ZooDefs; |
32 | |
|
33 | |
import java.util.concurrent.atomic.AtomicLong; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class MemoryObserver { |
40 | |
|
41 | 0 | public static final BooleanConfOption USE_MEMORY_OBSERVER = |
42 | |
new BooleanConfOption("giraph.memoryObserver.enabled", false, |
43 | |
"Whether or not to use memory observer"); |
44 | |
|
45 | 0 | public static final FloatConfOption FREE_MEMORY_FRACTION_FOR_GC = |
46 | |
new FloatConfOption("giraph.memoryObserver.freeMemoryFractionForGc", 0.1f, |
47 | |
"For which fraction of free memory will we issue manual gc calls"); |
48 | |
|
49 | 0 | public static final IntConfOption MIN_MS_BETWEEN_FULL_GCS = |
50 | |
new IntConfOption("giraph.memoryObserver.minMsBetweenFullGcs", 60 * 1000, |
51 | |
"Minimum milliseconds between two manual gc calls"); |
52 | |
|
53 | |
|
54 | 0 | private static final Logger LOG = Logger.getLogger(MemoryObserver.class); |
55 | |
|
56 | |
private static final int MEMORY_OBSERVER_SLEEP_MS = 1000; |
57 | |
|
58 | |
|
59 | 0 | private final AtomicLong lastManualGc = new AtomicLong(); |
60 | |
|
61 | |
private final ZooKeeperExt zk; |
62 | |
|
63 | |
private final String zkPath; |
64 | |
|
65 | |
private final int minMsBetweenFullGcs; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public MemoryObserver(final ZooKeeperExt zk, |
75 | 0 | final String zkPath, GiraphConfiguration conf) { |
76 | 0 | this.zk = zk; |
77 | 0 | this.zkPath = zkPath; |
78 | 0 | minMsBetweenFullGcs = MIN_MS_BETWEEN_FULL_GCS.get(conf); |
79 | |
|
80 | 0 | if (!USE_MEMORY_OBSERVER.get(conf)) { |
81 | 0 | return; |
82 | |
} |
83 | |
|
84 | |
try { |
85 | |
|
86 | 0 | zk.createOnceExt(zkPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, |
87 | |
CreateMode.PERSISTENT, true); |
88 | 0 | } catch (KeeperException | InterruptedException e) { |
89 | 0 | LOG.info("Exception occurred", e); |
90 | 0 | } |
91 | 0 | setWatcher(); |
92 | |
|
93 | 0 | final float freeMemoryFractionForGc = |
94 | 0 | FREE_MEMORY_FRACTION_FOR_GC.get(conf); |
95 | 0 | ThreadUtils.startThread(new Runnable() { |
96 | |
@Override |
97 | |
public void run() { |
98 | |
|
99 | |
while (true) { |
100 | 0 | double freeMemoryFraction = MemoryUtils.freeMemoryFraction(); |
101 | 0 | long msFromLastGc = System.currentTimeMillis() - lastManualGc.get(); |
102 | 0 | if (msFromLastGc > minMsBetweenFullGcs && |
103 | |
freeMemoryFraction < freeMemoryFractionForGc) { |
104 | |
try { |
105 | 0 | if (LOG.isInfoEnabled()) { |
106 | 0 | LOG.info("Notifying others about low memory (" + |
107 | |
freeMemoryFraction + "% free)"); |
108 | |
} |
109 | 0 | zk.createExt( |
110 | 0 | zkPath + "/" + System.currentTimeMillis(), |
111 | |
new byte[0], |
112 | |
ZooDefs.Ids.OPEN_ACL_UNSAFE, |
113 | |
CreateMode.EPHEMERAL, |
114 | |
false); |
115 | 0 | } catch (KeeperException | InterruptedException e) { |
116 | 0 | LOG.warn("Exception occurred", e); |
117 | 0 | } |
118 | |
} |
119 | 0 | if (!ThreadUtils.trySleep(MEMORY_OBSERVER_SLEEP_MS)) { |
120 | 0 | return; |
121 | |
} |
122 | 0 | } |
123 | |
} |
124 | |
}, "memory-observer"); |
125 | 0 | } |
126 | |
|
127 | |
|
128 | |
private void setWatcher() { |
129 | |
try { |
130 | |
|
131 | 0 | zk.getChildrenExt(zkPath, true, false, false); |
132 | 0 | } catch (KeeperException | InterruptedException e) { |
133 | 0 | LOG.warn("Exception occurred", e); |
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
public void callGc() { |
139 | 0 | long last = lastManualGc.get(); |
140 | 0 | if (System.currentTimeMillis() - last > minMsBetweenFullGcs && |
141 | 0 | lastManualGc.compareAndSet(last, System.currentTimeMillis())) { |
142 | 0 | if (LOG.isInfoEnabled()) { |
143 | 0 | LOG.info("Calling gc manually"); |
144 | |
} |
145 | 0 | System.gc(); |
146 | 0 | if (LOG.isInfoEnabled()) { |
147 | 0 | LOG.info("Manual gc call done"); |
148 | |
} |
149 | |
} |
150 | 0 | setWatcher(); |
151 | 0 | } |
152 | |
} |