use of io.fabric8.groups.Group in project fabric8 by jboss-fuse.
the class GroupTest method testGroupClose.
// Tests that if close() is executed right after start(), there are no left over entries.
// (see https://github.com/jboss-fuse/fuse/issues/133)
@Test
public void testGroupClose() throws Exception {
int port = findFreePort();
NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + port).connectionTimeoutMs(6000).sessionTimeoutMs(6000).retryPolicy(new RetryNTimes(10, 100));
CuratorFramework curator = builder.build();
curator.start();
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
String groupNode = "/singletons/test" + System.currentTimeMillis();
curator.create().creatingParentsIfNeeded().forPath(groupNode);
for (int i = 0; i < 100; i++) {
ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, groupNode, NodeState.class);
group.add(listener);
group.update(new NodeState("foo"));
group.start();
group.close();
List<String> entries = curator.getChildren().forPath(groupNode);
assertTrue(entries.isEmpty() || group.isUnstable());
if (group.isUnstable()) {
// let's wait for session timeout
curator.close();
curator = builder.build();
curator.start();
curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
}
}
curator.close();
cnxnFactory.shutdown();
cnxnFactory.join();
}
use of io.fabric8.groups.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();
}
use of io.fabric8.groups.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);
}
use of io.fabric8.groups.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);
}
}
}
}
}
}
}
use of io.fabric8.groups.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;
}
Aggregations