Repository navigation
Expand file tree
/
Copy pathClusterSynchronizerFactory.java
More file actions
151 lines (136 loc) · 5.94 KB
/
Copy pathClusterSynchronizerFactory.java
File metadata and controls
151 lines (136 loc) · 5.94 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
/*******************************************************************************
* 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.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import prerna.auth.utils.UserAssetUtils;
import prerna.cluster.sync.IClusterSynchronizer;
import prerna.engine.impl.SmssUtilities;
import prerna.util.Constants;
import prerna.util.EngineUtility;
import prerna.util.InternalUtility;
/**
* Selects and supplies the active {@link IClusterSynchronizer} implementation
* based on which cluster-sync flag is enabled ({@code SEMOSS_IS_CLUSTER_REDIS}
* or {@code SEMOSS_IS_CLUSTER_ZK}). This is the single entry point callers use
* so the rest of the codebase stays agnostic of the underlying coordination
* technology.
*/
public class ClusterSynchronizerFactory {
private static final Logger classLogger = LogManager.getLogger(ClusterSynchronizerFactory.class);
/** Whether the Redis-backed synchronizer is enabled. */
private static final boolean IS_REDIS = Boolean
.parseBoolean(IClusterSynchronizer.CLOUD_PROPS.get(IClusterSynchronizer.SEMOSS_IS_CLUSTER_REDIS_KEY));
/** Whether the ZooKeeper-backed synchronizer is enabled. */
private static final boolean IS_ZK = Boolean
.parseBoolean(IClusterSynchronizer.CLOUD_PROPS.get(IClusterSynchronizer.SEMOSS_IS_CLUSTER_ZK_KEY));
/** {@code true} when any cluster-synchronization backend is configured. */
public static final boolean IS_CLUSTER_SYNC_SETUP = IS_REDIS || IS_ZK;
static {
if (IS_REDIS && IS_ZK) {
classLogger.warn("Both '{}' and '{}' are enabled; defaulting to Redis for cluster synchronization.",
IClusterSynchronizer.SEMOSS_IS_CLUSTER_REDIS_KEY, IClusterSynchronizer.SEMOSS_IS_CLUSTER_ZK_KEY);
}
}
/**
* Returns the active cluster synchronizer for the configured backend. Redis
* takes precedence when both flags are enabled.
*
* @return the {@link IClusterSynchronizer} for the enabled backend
* @throws IllegalStateException if no cluster-sync backend is configured
* @throws Exception if the selected synchronizer fails to
* initialize
*/
public static IClusterSynchronizer getClusterSynchronizer() throws Exception {
if (IS_REDIS) {
return RedisClusterSynchronizer.getInstance();
}
if (IS_ZK) {
return ZKClusterSynchronizer.getInstance();
}
throw new IllegalStateException(
"No cluster synchronizer is configured. Enable '" + IClusterSynchronizer.SEMOSS_IS_CLUSTER_REDIS_KEY
+ "' or '" + IClusterSynchronizer.SEMOSS_IS_CLUSTER_ZK_KEY + "'.");
}
/**
* Determines whether a project is currently loaded on this node.
*
* @param projectId project identifier
* @return {@code true} if loaded and should be refreshed, otherwise
* {@code false}
*/
static boolean projectLoaded(String projectId) {
String projects = InternalUtility.getProjectIds() + "";
if (projects.startsWith(projectId) || projects.contains(";" + projectId + ";")
|| projects.endsWith(";" + projectId)) {
classLogger.info("Loaded project {} is out of date. Pulling latest changes", projectId);
return true;
} else {
return false;
}
}
/**
* Determines whether an engine is currently loaded on this node.
*
* @param engineId engine identifier
* @return {@code true} if loaded and should be refreshed, otherwise
* {@code false}
*/
static boolean engineLoaded(String engineId) {
String engines = InternalUtility.getEngineIds() + "";
if (engines.startsWith(engineId) || engines.contains(";" + engineId + ";")
|| engines.endsWith(";" + engineId)) {
classLogger.info("Loaded engine {} is out of date. Pulling latest changes", engineId);
return true;
} else {
return false;
}
}
/**
* Determines whether a user asset project is present locally on this node.
*
* @param projectId user asset project identifier
* @return {@code true} if local files indicate loaded state, otherwise
* {@code false}
*/
static boolean userLoaded(String projectId) {
// User assets are not registered in DIHelper like projects/engines.
// Check if the SMSS file exists locally it is only written here by
// pullUserAsset, meaning this pod has previously fetched this
// user's data and may have a stale copy.
String userFolder = EngineUtility.getUserFolder();
String assetSmss = userFolder + "/" + SmssUtilities.getUniqueName(UserAssetUtils.ASSET_APP_NAME, projectId)
+ ".smss";
if (new File(assetSmss).exists()) {
classLogger.info("User asset {} is out of date. Pulling latest changes", projectId);
return true;
}
return false;
}
}