use of io.fabric8.zookeeper.ZkPath in project fabric8 by jboss-fuse.
the class GatewayServiceTreeCacheTest method treeCacheEvent.
@Test
public void treeCacheEvent() throws Exception {
String zkPath = "/fabric/registry/clusters/test";
ServiceMap serviceMap = new ServiceMap();
CuratorFramework curator = mock(CuratorFramework.class);
GatewayServiceTreeCache cache = new GatewayServiceTreeCache(curator, zkPath, serviceMap);
String path = "/fabric/registry/clusters/test/default/0000001";
// Add container1 - master
// Add container2 - slave
cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container1", "service1", "service2")));
cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
assertEquals(1, serviceMap.getServices("default").size());
// Remove container1
// Update container2 - master
// Add container1 - slave
cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container1", "service1", "service2")));
cache.treeCacheEvent(event(path, CHILD_UPDATED, data("test", "container2", "service1", "service2")));
cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container1")));
assertEquals(1, serviceMap.getServices("default").size());
// Remove container2
// Update container1 - master
// Add container2 - slave
cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container2", "service1", "service2")));
cache.treeCacheEvent(event(path, CHILD_UPDATED, data("test", "container1", "service1", "service2")));
cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
assertEquals(1, serviceMap.getServices("default").size());
// Remove container2
// Add container2 - slave
cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container2")));
cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
assertEquals(1, serviceMap.getServices("default").size());
}
use of io.fabric8.zookeeper.ZkPath in project fabric8 by jboss-fuse.
the class HttpMappingZooKeeperTreeCache method treeCacheEvent.
protected void treeCacheEvent(PathChildrenCacheEvent event) {
String zkPath = zooKeeperPath;
ChildData childData = event.getData();
if (childData == null) {
return;
}
String path = childData.getPath();
Type type = event.getType();
byte[] data = childData.getData();
if (data == null || data.length == 0 || path == null) {
return;
}
if (path.startsWith(zkPath)) {
path = path.substring(zkPath.length());
}
// TODO should we remove the version too and pick that one?
// and include the version in the service chooser?
boolean remove = false;
switch(type) {
case CHILD_ADDED:
case CHILD_UPDATED:
break;
case CHILD_REMOVED:
remove = true;
break;
default:
return;
}
ServiceDTO dto = null;
try {
dto = mapper.readValue(data, ServiceDTO.class);
expandPropertyResolvers(dto);
List<String> services = dto.getServices();
Map<String, String> params = new HashMap<String, String>();
params.put("id", paramValue(dto.getId()));
params.put("container", paramValue(dto.getContainer()));
params.put("version", paramValue(dto.getVersion()));
params.put("bundleName", paramValue(dto.getBundleName()));
params.put("bundleVersion", paramValue(dto.getBundleVersion()));
mappingRuleConfiguration.updateMappingRules(remove, path, services, params, dto);
} catch (IOException e) {
LOG.warn("Failed to parse the JSON: " + new String(data) + ". Reason: " + e, e);
} catch (URISyntaxException e) {
LOG.warn("Failed to update URI for dto: " + dto + ", .Reason: " + e, e);
}
}
use of io.fabric8.zookeeper.ZkPath in project fabric8 by jboss-fuse.
the class DeploymentAgent method handleRestartJvmFlag.
/**
* Adds support for a directive to force a restart upon the first assignment of a specific profile to a container.
* It creates an entry in zk so that a subsequent modification to the same profile, will not trigger a jvm restart.
* The behavior is useful for situation when a profile provision .jars in lib/ folder, that are picked up only at
* jvm boot time.
*
* @param profile
* @param restart
* @return
*/
protected boolean handleRestartJvmFlag(Profile profile, AtomicBoolean restart) {
boolean result = false;
List<String> profilesRequiringRestart = new ArrayList<>();
ServiceReference<CuratorFramework> curatorServiceReference = systemBundleContext.getServiceReference(CuratorFramework.class);
ServiceReference<FabricService> fabricServiceReference = systemBundleContext.getServiceReference(FabricService.class);
if (curatorServiceReference != null && fabricServiceReference != null) {
CuratorFramework curator = systemBundleContext.getService(curatorServiceReference);
FabricService fs = systemBundleContext.getService(fabricServiceReference);
String currentContainerName = fs.getCurrentContainerName();
List<String> activeProfiles = fs.getCurrentContainer().getProfileIds();
// check for jvm restart requests
Map<String, String> agentProperties = profile.getConfiguration("io.fabric8.agent");
Map<String, String> jvmRestartEntries = new HashMap<>();
for (String key : agentProperties.keySet()) {
if (key.startsWith("io.fabric8.agent.forceOneTimeJVMRestart")) {
jvmRestartEntries.put(key, agentProperties.get(key));
LOGGER.info("Found a profile carrying a one-time JVM restart request: {}", key);
}
}
// clean old entries
String basePath = ZkPath.CONTAINER_PROVISION_RESTART.getPath(currentContainerName);
try {
if (ZooKeeperUtils.exists(curator, basePath) != null) {
List<String> zkPaths = ZooKeeperUtils.getAllChildren(curator, ZkPath.CONTAINER_PROVISION_RESTART.getPath(currentContainerName));
for (String zkPath : zkPaths) {
String[] split = zkPath.split("/");
String prof = split[split.length - 1];
if (!activeProfiles.contains(prof)) {
LOGGER.info("Deleting old JVM restart request status: {}", zkPath);
ZooKeeperUtils.delete(curator, zkPath);
}
}
}
} catch (Exception e) {
LOGGER.error("Unable to check ZK connection", e);
}
for (String key : jvmRestartEntries.keySet()) {
String[] split = key.split("\\.");
String profileForcingRestart = split[split.length - 1];
try {
String zkPath = ZkPath.CONTAINER_PROVISION_RESTART_PROFILES.getPath(currentContainerName, profileForcingRestart);
Stat exists = exists(curator, zkPath);
if (exists == null) {
ZooKeeperUtils.create(curator, zkPath);
profilesRequiringRestart.add(profileForcingRestart);
result = true;
}
} catch (Exception e) {
LOGGER.error("Unable to check ZK connection", e);
}
}
}
if (result) {
System.setProperty("karaf.restart.jvm", "true");
restart.set(true);
LOGGER.warn("Profiles {} scheduled a JVM restart request. Automated JVM restart support is not universally available. If your jvm doesn't support it you are required to manually restart the container that has just been assigned the profile.", profilesRequiringRestart);
try {
bundleContext.getBundle(0).stop();
} catch (BundleException e) {
LOGGER.error("Error when forcing a JVM restart", e);
}
}
return result;
}
use of io.fabric8.zookeeper.ZkPath in project fabric8 by jboss-fuse.
the class ZkDataStoreImpl method getAutoScaleStatus.
@Override
public AutoScaleStatus getAutoScaleStatus() {
assertValid();
try {
AutoScaleStatus answer = null;
String zkPath = ZkPath.AUTO_SCALE_STATUS.getPath();
if (configCache.getCurrentData(zkPath) != null) {
String json = getStringData(configCache, zkPath);
answer = RequirementsJson.autoScaleStatusFromJSON(json);
}
if (answer == null) {
answer = new AutoScaleStatus();
}
return answer;
} catch (Exception e) {
throw FabricException.launderThrowable(e);
}
}
use of io.fabric8.zookeeper.ZkPath in project fabric8 by jboss-fuse.
the class AutoScaleController method autoScale.
private void autoScale() {
FabricService service = fabricService.get();
FabricRequirements requirements = service.getRequirements();
List<ProfileRequirements> profileRequirements = requirements.getProfileRequirements();
if (profileRequirements != null && !profileRequirements.isEmpty()) {
AutoScaleStatus status = new AutoScaleStatus();
for (ProfileRequirements profileRequirement : profileRequirements) {
ContainerAutoScaler autoScaler = createAutoScaler(requirements, profileRequirement);
if (autoScaler != null) {
autoScaleProfile(service, autoScaler, requirements, profileRequirement, status);
} else {
LOGGER.warn("No ContainerAutoScaler available for profile " + profileRequirement.getProfile());
}
}
if (zkMasterCache != null) {
try {
String json = RequirementsJson.toJSON(status);
String zkPath = ZkPath.AUTO_SCALE_STATUS.getPath();
zkMasterCache.setStringData(zkPath, json, CreateMode.EPHEMERAL);
} catch (Exception e) {
LOGGER.warn("Failed to write autoscale status " + e, e);
}
} else {
LOGGER.warn("No ZooKeeperMasterCache!");
}
}
}
Aggregations