Repository navigation
Expand file tree
/
Copy pathZKClusterSynchronizer.java
More file actions
296 lines (261 loc) · 11.4 KB
/
Copy pathZKClusterSynchronizer.java
File metadata and controls
296 lines (261 loc) · 11.4 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*******************************************************************************
* Copyright 2015 Defense Health Agency (DHA)
*
* If your use of this software does not include any GPLv2 components:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------
* If your use of this software includes any GPLv2 components:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*******************************************************************************/
package prerna.cluster.sync.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.CuratorCache;
import org.apache.curator.framework.recipes.cache.CuratorCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.RetryNTimes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import prerna.cluster.sync.IClusterSynchronizer;
import prerna.cluster.util.ClusterSyncMethod;
public class ZKClusterSynchronizer implements IClusterSynchronizer {
private static volatile ZKClusterSynchronizer sync = null;
private static final Logger classLogger = LogManager.getLogger(ZKClusterSynchronizer.class);
private static final String ZK_SERVER_STRING = "ZK_SERVER";
private static final String SYNC_PROJECT_PATH = "/sync/project";
private static final String SYNC_ENGINE_PATH = "/sync/engine";
private static final String SYNC_USER_PATH = "/sync/user";
private CuratorFramework client = null;
private CuratorCache projectCache;
private CuratorCache engineCache;
private CuratorCache userCache;
/**
* Creates a synchronizer and immediately initializes ZooKeeper connectivity and
* listeners.
*/
private ZKClusterSynchronizer() {
initalizeClusterSyncronizer();
}
/**
* Returns the singleton synchronizer instance.
*
* @return singleton {@link ZKClusterSynchronizer}
* @throws Exception if synchronizer initialization fails
*/
static ZKClusterSynchronizer getInstance() throws Exception {
if (sync != null) {
return sync;
}
if (sync == null) {
synchronized (ZKClusterSynchronizer.class) {
if (sync != null) {
return sync;
}
sync = new ZKClusterSynchronizer();
}
}
return sync;
}
/**
* Initializes Curator client state and ensures the watched synchronization
* paths exist in ZooKeeper.
*/
private void initalizeClusterSyncronizer() {
classLogger.info("Starting up cluster synchronizer");
// hostname:port, localhost:2181
String zkServerString = CLOUD_PROPS.get(ZK_SERVER_STRING);
if (zkServerString == null || zkServerString.isEmpty()) {
throw new IllegalArgumentException("Zookeeper Server endpoint is not defined");
}
// make the curator
try {
client = CuratorFrameworkFactory.newClient(zkServerString, new RetryNTimes(3, 10));
client.start();
// Check if the ZNode exists before trying to create it - project
if (client.checkExists().forPath(SYNC_PROJECT_PATH) == null) {
client.create().creatingParentsIfNeeded().forPath(SYNC_PROJECT_PATH);
}
// Check if the ZNode exists before trying to create it - engine
if (client.checkExists().forPath(SYNC_ENGINE_PATH) == null) {
client.create().creatingParentsIfNeeded().forPath(SYNC_ENGINE_PATH);
}
// Check if the ZNode exists before trying to create it - user
if (client.checkExists().forPath(SYNC_USER_PATH) == null) {
client.create().creatingParentsIfNeeded().forPath(SYNC_USER_PATH);
}
projectCache = createCacheListener(SYNC_PROJECT_PATH);
engineCache = createCacheListener(SYNC_ENGINE_PATH);
userCache = createCacheListener(SYNC_USER_PATH);
} catch (Exception e) {
classLogger.error(
"Failed to initialize ClusterSynchronizer with zkServer='{}' and host='{}'. Watch paths: '{}', '{}', '{}'.",
zkServerString, CONTAINER_IP, SYNC_PROJECT_PATH, SYNC_ENGINE_PATH, SYNC_USER_PATH, e);
}
}
/**
* Creates, starts, and wires a {@link CuratorCache} listener for a sync root
* path.
*
* @param pathToWatch path root to monitor for child updates
* @return initialized cache for the supplied path
*/
private CuratorCache createCacheListener(String pathToWatch) {
CuratorCache cache = CuratorCache.build(client, pathToWatch);
CuratorCacheListener listener = CuratorCacheListener.builder()
.forPathChildrenCache(pathToWatch, client, new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) {
ByteArrayInputStream byteIn = new ByteArrayInputStream(event.getData().getData());
ObjectInputStream in = new ObjectInputStream(byteIn);
ClusterSyncEvent syncEvent = (ClusterSyncEvent) in.readObject();
String updatedByNodeId = syncEvent.getNodeId();
// if the host updated it, then its already ready - other nodes have to pull
if (!CONTAINER_IP.equals(updatedByNodeId)) {
String fullPath = event.getData().getPath();
classLogger.info("{} updated, pulling latest data from cloud storage", fullPath);
String id = null;
boolean pull = false;
if (fullPath.startsWith(SYNC_PROJECT_PATH)) {
String[] path = fullPath.split(SYNC_PROJECT_PATH + "/");
id = path[1];
pull = ClusterSynchronizerFactory.projectLoaded(id);
} else if (fullPath.startsWith(SYNC_USER_PATH)) {
String[] path = fullPath.split(SYNC_USER_PATH + "/");
id = path[1];
pull = ClusterSynchronizerFactory.userLoaded(id);
} else {
String[] path = fullPath.split(SYNC_ENGINE_PATH + "/");
id = path[1];
pull = ClusterSynchronizerFactory.engineLoaded(id);
}
// always check if the engine has been loaded before pulling.
if (pull) {
try {
List<Object> params = syncEvent.getParams();
String methodToken = syncEvent.getMethodName();
ClusterSyncMethod method = ClusterSyncMethod.fromWireName(methodToken);
if (method == null) {
classLogger.error(
"Unrecognized cluster sync method '{}' for path='{}', id='{}'. Skipping.",
methodToken, fullPath, id);
} else {
method.apply(params.toArray());
}
} catch (Exception e) {
classLogger.error(
"Failed to process cluster sync update for path='{}', id='{}', method='{}', params='{}'.",
fullPath, id, syncEvent.getMethodName(), syncEvent.getParams(), e);
}
}
}
}
}
}).build();
cache.listenable().addListener(listener);
cache.start();
return cache;
}
/**
* Publishes an engine change event to ZooKeeper so other nodes can pull
* updates.
*
* @param engineId engine identifier
* @param method the {@link ClusterSyncMethod} peers should run to pull the
* update
* @param params serialized method arguments
* @throws Exception if path creation, serialization, or publish fails
*/
@Override
public void publishEngineChange(String engineId, ClusterSyncMethod method, Object... params) throws Exception {
String enginePath = SYNC_ENGINE_PATH + "/" + engineId;
// this creates the path if it doesn't exist
if (client.checkExists().forPath(enginePath) == null) {
client.create().creatingParentsIfNeeded().forPath(enginePath);
}
classLogger.info("Publishing change for engine {} and for nodes to {}", engineId, method.getWireName());
performWrite(enginePath, method, params);
}
/**
* Publishes a user asset change event to ZooKeeper so other nodes can pull
* updates.
*
* @param projectId user asset project identifier
* @param method the {@link ClusterSyncMethod} peers should run to pull the
* update
* @param params serialized method arguments
* @throws Exception if path creation, serialization, or publish fails
*/
@Override
public void publishUserChange(String projectId, ClusterSyncMethod method, Object... params) throws Exception {
String userPath = SYNC_USER_PATH + "/" + projectId;
// this creates the path if it doesn't exist
if (client.checkExists().forPath(userPath) == null) {
client.create().creatingParentsIfNeeded().forPath(userPath);
}
classLogger.info("Publishing change for user asset {} and for nodes to {}", projectId, method.getWireName());
performWrite(userPath, method, params);
}
/**
* Publishes a project change event to ZooKeeper so other nodes can pull
* updates.
*
* @param projectId project identifier
* @param method the {@link ClusterSyncMethod} peers should run to pull the
* update
* @param params serialized method arguments
* @throws Exception if path creation, serialization, or publish fails
*/
@Override
public void publishProjectChange(String projectId, ClusterSyncMethod method, Object... params) throws Exception {
String projectPath = SYNC_PROJECT_PATH + "/" + projectId;
// this creates the path if it doesn't exist
if (client.checkExists().forPath(projectPath) == null) {
client.create().creatingParentsIfNeeded().forPath(projectPath);
}
classLogger.info("Publishing change for project {} and for nodes to {}", projectId, method.getWireName());
performWrite(projectPath, method, params);
}
/**
* Serializes the change payload ({@code nodeId}, wire method name, and params)
* and writes it to the given ZooKeeper node, triggering the watchers on peer
* nodes.
*
* @param path ZooKeeper node path to write the event to
* @param method the {@link ClusterSyncMethod} peers should run to pull the
* update
* @param params serialized method arguments
* @throws Exception if serialization or the ZooKeeper write fails
*/
private void performWrite(String path, ClusterSyncMethod method, Object... params) throws Exception {
ClusterSyncEvent syncEvent = new ClusterSyncEvent(CONTAINER_IP, path, method.getWireName(), params);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(syncEvent);
client.setData().forPath(path, byteOut.toByteArray());
}
}