Search in sources :

Example 1 with ChildData

use of io.fabric8.groups.internal.ChildData in project fabric8 by jboss-fuse.

the class ZooKeeperGroupTest method putChildData.

private static void putChildData(ZooKeeperGroup<NodeState> group, String path, String container) throws Exception {
    NodeState node = new NodeState("test", container);
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).writeValue(data, node);
    ChildData<NodeState> child = new ChildData<>(path, new Stat(), data.toByteArray(), node);
    group.currentData.put(path, child);
}
Also used : NodeState(io.fabric8.groups.NodeState) Stat(org.apache.zookeeper.data.Stat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ChildData

use of io.fabric8.groups.internal.ChildData in project fabric8 by jboss-fuse.

the class GatewayServiceTreeCache method treeCacheEvent.

protected void treeCacheEvent(PathChildrenCacheEvent event) {
    ChildData childData = event.getData();
    if (childData == null) {
        return;
    }
    String path = childData.getPath();
    LOG.trace("Event {} on path {}", event.getType().toString(), path);
    PathChildrenCacheEvent.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());
    }
    List<String> split = Strings.splitAndTrimAsList(path, "/");
    // Lets just use the group name as the service path.
    path = split.get(0);
    String zNode = split.get(1);
    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);
        dto.setContainer(dto.getContainer() + "_zNode_" + zNode);
        if (remove) {
            LOG.info("Removed gateway service: " + path + ": " + dto);
            serviceMap.serviceRemoved(path, dto);
        } else {
            LOG.info("Updated gateway service: " + path + ": " + dto);
            serviceMap.serviceUpdated(path, 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 : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 3 with ChildData

use of io.fabric8.groups.internal.ChildData in project fabric8 by jboss-fuse.

the class GatewayServiceTreeCache method treeCacheEvent.

protected void treeCacheEvent(PathChildrenCacheEvent event) {
    ChildData childData = event.getData();
    if (childData == null) {
        return;
    }
    String path = childData.getPath();
    PathChildrenCacheEvent.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());
    }
    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);
        if (remove) {
            serviceMap.serviceRemoved(path, dto);
        } else {
            serviceMap.serviceUpdated(path, 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 : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 4 with ChildData

use of io.fabric8.groups.internal.ChildData 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 5 with ChildData

use of io.fabric8.groups.internal.ChildData in project fabric8 by jboss-fuse.

the class ZooKeeperMultiGroup method isMaster.

@Override
public boolean isMaster(String name) {
    List<ChildData<T>> children = getActiveChildren();
    Collections.sort(children, sequenceComparator);
    for (ChildData child : children) {
        NodeState node = (NodeState) child.getNode();
        if (node.id.equals(name)) {
            if (child.getPath().equals(getId())) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}
Also used : NodeState(io.fabric8.groups.NodeState)

Aggregations

ServiceDTO (io.fabric8.gateway.ServiceDTO)3 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 ChildData (org.apache.curator.framework.recipes.cache.ChildData)3 NodeState (io.fabric8.groups.NodeState)2 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ChildData (io.fabric8.groups.internal.ChildData)1 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 Type (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type)1 RetryNTimes (org.apache.curator.retry.RetryNTimes)1 Stat (org.apache.zookeeper.data.Stat)1 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)1 Test (org.junit.Test)1