Search in sources :

Example 6 with ZkPath

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());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceMap(io.fabric8.gateway.ServiceMap) Test(org.junit.Test)

Example 7 with ZkPath

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);
    }
}
Also used : Type(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 8 with ZkPath

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConfigurationException(org.osgi.service.cm.ConfigurationException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) CuratorFramework(org.apache.curator.framework.CuratorFramework) Stat(org.apache.zookeeper.data.Stat) FabricService(io.fabric8.api.FabricService) BundleException(org.osgi.framework.BundleException)

Example 9 with ZkPath

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);
    }
}
Also used : AutoScaleStatus(io.fabric8.api.AutoScaleStatus) FabricException(io.fabric8.api.FabricException) InvalidClassException(java.io.InvalidClassException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 10 with ZkPath

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!");
        }
    }
}
Also used : ProfileRequirements(io.fabric8.api.ProfileRequirements) FabricService(io.fabric8.api.FabricService) AutoScaleStatus(io.fabric8.api.AutoScaleStatus) ContainerAutoScaler(io.fabric8.api.ContainerAutoScaler) FabricRequirements(io.fabric8.api.FabricRequirements)

Aggregations

IOException (java.io.IOException)5 ServiceDTO (io.fabric8.gateway.ServiceDTO)3 URISyntaxException (java.net.URISyntaxException)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 ChildData (org.apache.curator.framework.recipes.cache.ChildData)3 AutoScaleStatus (io.fabric8.api.AutoScaleStatus)2 FabricService (io.fabric8.api.FabricService)2 ServiceMap (io.fabric8.gateway.ServiceMap)2 TcpGateway (io.fabric8.gateway.handlers.tcp.TcpGateway)2 LoadBalancer (io.fabric8.gateway.loadbalancer.LoadBalancer)2 ArrayList (java.util.ArrayList)2 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)2 ContainerAutoScaler (io.fabric8.api.ContainerAutoScaler)1 FabricException (io.fabric8.api.FabricException)1 FabricRequirements (io.fabric8.api.FabricRequirements)1 ProfileRequirements (io.fabric8.api.ProfileRequirements)1 HttpMappingRuleBase (io.fabric8.gateway.fabric.support.http.HttpMappingRuleBase)1 HttpMappingZooKeeperTreeCache (io.fabric8.gateway.fabric.support.http.HttpMappingZooKeeperTreeCache)1 VertxService (io.fabric8.gateway.fabric.support.vertx.VertxService)1 SimplePathTemplate (io.fabric8.zookeeper.internal.SimplePathTemplate)1