Search in sources :

Example 31 with Group

use of io.fabric8.openshift.api.model.Group in project fabric8 by jboss-fuse.

the class GroupTest method testAddFieldIgnoredOnParse.

@Test
public void testAddFieldIgnoredOnParse() throws Exception {
    int port = findFreePort();
    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    String groupNode = "/singletons/test" + System.currentTimeMillis();
    curator.create().creatingParentsIfNeeded().forPath(groupNode);
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    final ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, groupNode, NodeState.class);
    group.add(listener);
    group.start();
    GroupCondition groupCondition = new GroupCondition();
    group.add(groupCondition);
    group.update(new NodeState("foo"));
    assertTrue(groupCondition.waitForConnected(5, TimeUnit.SECONDS));
    assertTrue(groupCondition.waitForMaster(5, TimeUnit.SECONDS));
    ChildData currentData = group.getCurrentData().get(0);
    final int version = currentData.getStat().getVersion();
    NodeState lastState = group.getLastState();
    String json = lastState.toString();
    System.err.println("JSON:" + json);
    String newValWithNewField = json.substring(0, json.lastIndexOf('}')) + ",\"Rubbish\":\"Rubbish\"}";
    curator.getZookeeperClient().getZooKeeper().setData(group.getId(), newValWithNewField.getBytes(), version);
    assertTrue(group.isMaster());
    int attempts = 0;
    while (attempts++ < 5 && version == group.getCurrentData().get(0).getStat().getVersion()) {
        TimeUnit.SECONDS.sleep(1);
    }
    assertNotEquals("We see the updated version", version, group.getCurrentData().get(0).getStat().getVersion());
    System.err.println("CurrentData:" + group.getCurrentData());
    group.close();
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) ChildData(io.fabric8.groups.internal.ChildData) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) Test(org.junit.Test)

Example 32 with Group

use of io.fabric8.openshift.api.model.Group in project fabric8 by jboss-fuse.

the class GroupTest method testOrder.

@Test
public void testOrder() throws Exception {
    int port = findFreePort();
    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();
    final String path = "/singletons/test/Order" + System.currentTimeMillis();
    ArrayList<ZooKeeperGroup> members = new ArrayList<ZooKeeperGroup>();
    for (int i = 0; i < 4; i++) {
        ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, path, NodeState.class);
        group.add(listener);
        members.add(group);
    }
    for (ZooKeeperGroup group : members) {
        assertFalse(group.isConnected());
        assertFalse(group.isMaster());
    }
    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    // first to start should be master if members are ordered...
    int i = 0;
    for (ZooKeeperGroup group : members) {
        group.start();
        group.update(new NodeState("foo" + i));
        i++;
        // wait for registration
        while (group.getId() == null) {
            TimeUnit.MILLISECONDS.sleep(100);
        }
    }
    boolean firsStartedIsMaster = members.get(0).isMaster();
    for (ZooKeeperGroup group : members) {
        group.close();
    }
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
    assertTrue("first started is master", firsStartedIsMaster);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) ArrayList(java.util.ArrayList) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) Test(org.junit.Test)

Example 33 with Group

use of io.fabric8.openshift.api.model.Group in project fabric8 by jboss-fuse.

the class MQManager method addMasterSlaveStatus.

protected void addMasterSlaveStatus(List<MQBrokerStatusDTO> answer) throws Exception {
    Map<String, Map<String, MQBrokerStatusDTO>> groupMap = new HashMap<String, Map<String, MQBrokerStatusDTO>>();
    for (MQBrokerStatusDTO status : answer) {
        String key = status.getGroup();
        Map<String, MQBrokerStatusDTO> list = groupMap.get(key);
        if (list == null) {
            list = new HashMap<String, MQBrokerStatusDTO>();
            groupMap.put(key, list);
        }
        String statusPath = String.format("%s/%s", status.getContainer(), status.getBrokerName());
        list.put(statusPath, status);
    }
    CuratorFramework curator = getCurator();
    // now lets check the cluster status for each group
    Set<Map.Entry<String, Map<String, MQBrokerStatusDTO>>> entries = groupMap.entrySet();
    for (Map.Entry<String, Map<String, MQBrokerStatusDTO>> entry : entries) {
        String group = entry.getKey();
        Map<String, MQBrokerStatusDTO> containerMap = entry.getValue();
        String groupPath = ZkPath.MQ_CLUSTER.getPath(group);
        List<String> children = getChildrenSafe(curator, groupPath);
        for (String child : children) {
            String childPath = groupPath + "/" + child;
            byte[] data = curator.getData().forPath(childPath);
            if (data != null && data.length > 0) {
                String text = new String(data).trim();
                if (!text.isEmpty()) {
                    ObjectMapper mapper = new ObjectMapper();
                    Map<String, Object> map = mapper.readValue(data, HashMap.class);
                    String id = stringValue(map, "id", "container");
                    if (id != null) {
                        String container = stringValue(map, "container", "agent");
                        String statusPath = String.format("%s/%s", container, id);
                        MQBrokerStatusDTO containerStatus = containerMap.get(statusPath);
                        if (containerStatus != null) {
                            Boolean master = null;
                            List services = listValue(map, "services");
                            if (services != null) {
                                if (!services.isEmpty()) {
                                    List<String> serviceTexts = new ArrayList<String>();
                                    for (Object service : services) {
                                        String serviceText = getSubstitutedData(curator, service.toString());
                                        if (Strings.isNotBlank(serviceText)) {
                                            serviceTexts.add(serviceText);
                                        }
                                        containerStatus.setServices(serviceTexts);
                                    }
                                    master = Boolean.TRUE;
                                } else {
                                    master = Boolean.FALSE;
                                }
                            } else {
                                master = Boolean.FALSE;
                            }
                            containerStatus.setMaster(master);
                        }
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MQBrokerStatusDTO(io.fabric8.api.jmx.MQBrokerStatusDTO) CuratorFramework(org.apache.curator.framework.CuratorFramework) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 34 with Group

use of io.fabric8.openshift.api.model.Group in project fabric8 by jboss-fuse.

the class MQManager method createOrUpdateProfile.

/**
 * Creates or updates the broker profile for the given DTO and updates the requirements so that the
 * minimum number of instances of the profile is updated
 */
public static Profile createOrUpdateProfile(MQBrokerConfigDTO dto, FabricService fabricService, RuntimeProperties runtimeProperties) throws IOException {
    FabricRequirements requirements = fabricService.getRequirements();
    MQService mqService = createMQService(fabricService, runtimeProperties);
    Map<String, String> configuration = new HashMap<String, String>();
    List<String> properties = dto.getProperties();
    String version = dto.version();
    boolean changeInCurrentVersion = requirements.getVersion().equals(dto.getVersion());
    if (properties != null) {
        for (String entry : properties) {
            String[] parts = entry.split("=", 2);
            if (parts.length == 2) {
                configuration.put(parts[0], parts[1]);
            } else {
                configuration.put(parts[0], "");
            }
        }
    }
    String data = dto.getData();
    String profileName = dto.profile();
    try {
        FabricValidations.validateProfileName(profileName);
    } catch (IllegalArgumentException e) {
        // we do not want exception in the server log, so print the error message to the console
        System.out.println(e.getMessage());
        return null;
    }
    String brokerName = dto.getBrokerName();
    if (data == null) {
        // lets use a relative path so we work on any karaf container
        data = "${runtime.data}" + brokerName;
    }
    configuration.put(DATA, data);
    for (Map.Entry<String, String> port : dto.getPorts().entrySet()) {
        configuration.put(port.getKey() + "-port", port.getValue());
    }
    BrokerKind kind = dto.kind();
    configuration.put(KIND, kind.toString());
    String config = dto.getConfigUrl();
    if (config != null) {
        configuration.put(CONFIG_URL, mqService.getConfig(version, config));
    }
    String group = dto.getGroup();
    if (group != null) {
        configuration.put(GROUP, group);
    }
    Maps.setStringValues(configuration, NETWORKS, dto.getNetworks());
    String networksUserName = dto.getNetworksUserName();
    if (networksUserName != null) {
        configuration.put(NETWORK_USER_NAME, networksUserName);
    }
    String networksPassword = dto.getNetworksPassword();
    if (networksPassword != null) {
        configuration.put(NETWORK_PASSWORD, networksPassword);
    }
    String parentProfile = dto.getParentProfile();
    if (parentProfile != null) {
        configuration.put(PARENT, parentProfile);
    }
    Boolean ssl = dto.getSsl();
    if (ssl != null) {
        configuration.put(SSL, ssl.toString());
    }
    Integer replicas = dto.getReplicas();
    if (replicas != null) {
        configuration.put(REPLICAS, replicas.toString());
    }
    Integer minInstances = dto.getMinimumInstances();
    if (minInstances != null) {
        configuration.put(MINIMUM_INSTANCES, minInstances.toString());
    }
    Profile profile = mqService.createOrUpdateMQProfile(version, profileName, brokerName, configuration, dto.kind().equals(BrokerKind.Replicated));
    String profileId = profile.getId();
    ProfileRequirements profileRequirement = requirements.getOrCreateProfileRequirement(profileId);
    Integer minimumInstances = profileRequirement.getMinimumInstances();
    // lets reload the DTO as we may have inherited some values from the parent profile
    List<MQBrokerConfigDTO> list = createConfigDTOs(mqService, profile);
    // lets assume 2 required instances for master/slave unless folks use
    // N+1 or replicated
    int requiredInstances = 2;
    if (list.size() == 1) {
        MQBrokerConfigDTO loadedDTO = list.get(0);
        requiredInstances = loadedDTO.requiredInstances();
    } else {
        // assume N+1 broker as there's more than one broker in the profile; so lets set the required size to N+1
        requiredInstances = list.size() + 1;
    }
    if (changeInCurrentVersion && (minimumInstances == null || minimumInstances.intValue() < requiredInstances)) {
        profileRequirement.setMinimumInstances(requiredInstances);
        fabricService.setRequirements(requirements);
    }
    String clientProfile = dto.clientProfile();
    if (Strings.isNotBlank(clientProfile)) {
        String clientParentProfile = dto.getClientParentProfile();
        if (Strings.isNullOrBlank(clientParentProfile)) {
            clientParentProfile = "mq-client-base";
        }
        mqService.createOrUpdateMQClientProfile(version, clientProfile, group, clientParentProfile);
    }
    return profile;
}
Also used : MQService(io.fabric8.api.MQService) ProfileRequirements(io.fabric8.api.ProfileRequirements) BrokerKind(io.fabric8.api.jmx.BrokerKind) HashMap(java.util.HashMap) Profile(io.fabric8.api.Profile) MQBrokerConfigDTO(io.fabric8.api.jmx.MQBrokerConfigDTO) FabricRequirements(io.fabric8.api.FabricRequirements) Map(java.util.Map) HashMap(java.util.HashMap)

Example 35 with Group

use of io.fabric8.openshift.api.model.Group in project fabric8 by jboss-fuse.

the class MQServiceImpl method createOrUpdateMQClientProfile.

@Override
public Profile createOrUpdateMQClientProfile(String versionId, String profileId, String group, String parentProfileName) {
    Version version = profileService.getRequiredVersion(versionId);
    Profile parentProfile = null;
    if (Strings.isNotBlank(parentProfileName)) {
        parentProfile = version.getRequiredProfile(parentProfileName);
    }
    if (group == null || profileId == null)
        return parentProfile;
    ProfileBuilder builder;
    // create a profile if it doesn't exist
    boolean create = !version.hasProfile(profileId);
    if (create) {
        builder = ProfileBuilder.Factory.create(versionId, profileId);
    } else {
        Profile profile = version.getRequiredProfile(profileId);
        builder = ProfileBuilder.Factory.createFrom(profile);
    }
    // set the parent if its specified
    if (parentProfile != null) {
        builder.addParent(parentProfile.getId());
    }
    Map<String, String> config = builder.getConfiguration(MQ_CONNECTION_FACTORY_PID);
    config.put(GROUP, group);
    builder.addConfiguration(MQ_CONNECTION_FACTORY_PID, config);
    Profile profile = builder.getProfile();
    return create ? profileService.createProfile(profile) : profileService.updateProfile(profile);
}
Also used : Version(io.fabric8.api.Version) ProfileBuilder(io.fabric8.api.ProfileBuilder) Profile(io.fabric8.api.Profile)

Aggregations

Test (org.junit.Test)18 CuratorFramework (org.apache.curator.framework.CuratorFramework)12 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 RetryNTimes (org.apache.curator.retry.RetryNTimes)8 NodeState (io.fabric8.groups.NodeState)7 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)6 File (java.io.File)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Profile (io.fabric8.api.Profile)4 GitNode (io.fabric8.git.GitNode)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 RuntimeProperties (io.fabric8.api.RuntimeProperties)3 MQBrokerConfigDTO (io.fabric8.api.jmx.MQBrokerConfigDTO)3 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)3 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)3 TcpGateway (io.fabric8.gateway.handlers.tcp.TcpGateway)2 Group (io.fabric8.groups.Group)2