@Test public void testDetermineSyncActions() { String externalIdA = setupSync("rancher-running-host-stopped", false, STATE_RUNNING, STATE_STOPPED); String externalIdC = setupSync("rancher-running-host-stopped", true, STATE_RUNNING, STATE_STOPPED); String externalIdD = setupSync("rancher-running-host-destroyed", false, STATE_RUNNING, null); String externalIdE = setupSync("rancher-running-host-destroyed", true, STATE_RUNNING, null); String externalIdH = setupSync("rancher-stopped-host-running", false, STATE_STOPPED, STATE_RUNNING); String externalIdI = setupSync("rancher-stopped-host-running", true, STATE_STOPPED, STATE_RUNNING); String externalIdK = setupSync("rancher-stopped-host-destroyed", false, STATE_STOPPED, null); String externalIdL = setupSync("rancher-stopped-host-destroyed", true, STATE_STOPPED, null); String uuidK = uuid("rancher-stopped-host-destroyed", false); String externalIdO = setupSync("rancher-removed-host-running", false, REMOVED, STATE_RUNNING); String externalIdP = setupSync("rancher-removed-host-running", true, REMOVED, STATE_RUNNING); String externalIdS = setupSync("rancher-removed-host-stopped", false, REMOVED, STATE_STOPPED); String externalIdT = setupSync("rancher-removed-host-stopped", true, REMOVED, STATE_STOPPED); String uuidS = uuid("rancher-removed-host-stopped", false); String uuidT = uuid("rancher-removed-host-stopped", false); String externalIdW = setupSync("rancher-removed-host-removed", false, REMOVED, null); String externalIdX = setupSync("rancher-removed-host-removed", true, REMOVED, null); String uuidW = uuid("rancher-removed-host-stopped", false); String uuidX = uuid("rancher-removed-host-stopped", false); String externalIdAA = setupSync("rancher-notexist-host-running", false, null, STATE_RUNNING); String externalIdBB = setupSync("rancher-notexist-host-running", true, null, STATE_RUNNING); String externalIdEE = setupSync("rancher-notexist-host-stopped", false, null, STATE_STOPPED); String externalIdFF = setupSync("rancher-notexist-host-stopped", true, null, STATE_STOPPED); monitor.determineSyncActions(knownInstances, reportedInstances, needsSynced, syncActions, false); assertSyncAction(externalIdA, EVENT_STOP); assertSyncAction(externalIdC, EVENT_STOP); assertTrue(!needsSynced.containsKey(externalIdD)); assertSyncAction(externalIdE, EVENT_DESTROY); assertSyncAction(externalIdH, EVENT_START); assertSyncAction(externalIdI, EVENT_START); assertDoNothing(externalIdK, uuidK); assertSyncAction(externalIdL, EVENT_DESTROY); assertSyncAction(externalIdO, EVENT_INSTANCE_FORCE_STOP); assertSyncAction(externalIdP, EVENT_INSTANCE_FORCE_STOP); assertDoNothing(externalIdS, uuidS); assertDoNothing(externalIdT, uuidT); assertDoNothing(externalIdW, uuidW); assertDoNothing(externalIdX, uuidX); assertSyncAction(externalIdAA, EVENT_START); assertSyncAction(externalIdBB, EVENT_START); assertSyncAction(externalIdEE, EVENT_START); assertSyncAction(externalIdFF, EVENT_START); }
void determineSyncActions(Map<String, KnownInstance> knownInstances, ReportedInstances reportedInstances, Map<String, ReportedInstance> needsSynced, Map<String, String> syncActions, boolean checkOnly) { Map<String, KnownInstance> inRancher = new HashMap<String, KnownInstance>(knownInstances); Map<String, ReportedInstance> onHost = new HashMap<String, ReportedInstance>(reportedInstances.byExternalId); for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byUuid.entrySet()) { KnownInstance ki = knownInstances.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getValue().getExternalId(), reported.getKey()); } } if (!onHost.isEmpty() || !inRancher.isEmpty()) { Map<String, KnownInstance> knownByExternalId = new HashMap<String, KnownInstance>(); for (KnownInstance ki : knownInstances.values()) { if (StringUtils.isNotEmpty(ki.getExternalId())) knownByExternalId.put(ki.getExternalId(), ki); } for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byExternalId.entrySet()) { KnownInstance ki = knownByExternalId.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getKey(), ki.getUuid()); } } } for (Map.Entry<String, ReportedInstance> create : onHost.entrySet()) { ReportedInstance ri = create.getValue(); addSyncAction(needsSynced, syncActions, ri, EVENT_START, checkOnly); } for (KnownInstance ki : inRancher.values()) { List<String> forRemove = Arrays.asList(CommonStatesConstants.REMOVING, InstanceConstants.STATE_ERROR, InstanceConstants.STATE_ERRORING); if (objectMetaDataManager.isTransitioningState(Instance.class, ki.getState()) || ki.getRemoved() != null || forRemove.contains(ki.getState()) || (STATE_STOPPED.equals(ki.getState()) && StringUtils.isEmpty(ki.getExternalId()))) continue; ReportedInstance ri = new ReportedInstance(); ri.setExternalId(ki.getExternalId()); ri.setUuid(ki.getUuid()); Object imageUuid = CollectionUtils.getNestedValue(ki.getData(), DataUtils.FIELDS, FIELD_IMAGE_UUID); String image = imageUuid != null ? imageUuid.toString() : null; ri.setImage(image); addSyncAction(needsSynced, syncActions, ri, EVENT_DESTROY, checkOnly); } }
PingInstancesMonitorImpl implements PingInstancesMonitor { void determineSyncActions(Map<String, KnownInstance> knownInstances, ReportedInstances reportedInstances, Map<String, ReportedInstance> needsSynced, Map<String, String> syncActions, boolean checkOnly) { Map<String, KnownInstance> inRancher = new HashMap<String, KnownInstance>(knownInstances); Map<String, ReportedInstance> onHost = new HashMap<String, ReportedInstance>(reportedInstances.byExternalId); for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byUuid.entrySet()) { KnownInstance ki = knownInstances.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getValue().getExternalId(), reported.getKey()); } } if (!onHost.isEmpty() || !inRancher.isEmpty()) { Map<String, KnownInstance> knownByExternalId = new HashMap<String, KnownInstance>(); for (KnownInstance ki : knownInstances.values()) { if (StringUtils.isNotEmpty(ki.getExternalId())) knownByExternalId.put(ki.getExternalId(), ki); } for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byExternalId.entrySet()) { KnownInstance ki = knownByExternalId.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getKey(), ki.getUuid()); } } } for (Map.Entry<String, ReportedInstance> create : onHost.entrySet()) { ReportedInstance ri = create.getValue(); addSyncAction(needsSynced, syncActions, ri, EVENT_START, checkOnly); } for (KnownInstance ki : inRancher.values()) { List<String> forRemove = Arrays.asList(CommonStatesConstants.REMOVING, InstanceConstants.STATE_ERROR, InstanceConstants.STATE_ERRORING); if (objectMetaDataManager.isTransitioningState(Instance.class, ki.getState()) || ki.getRemoved() != null || forRemove.contains(ki.getState()) || (STATE_STOPPED.equals(ki.getState()) && StringUtils.isEmpty(ki.getExternalId()))) continue; ReportedInstance ri = new ReportedInstance(); ri.setExternalId(ki.getExternalId()); ri.setUuid(ki.getUuid()); Object imageUuid = CollectionUtils.getNestedValue(ki.getData(), DataUtils.FIELDS, FIELD_IMAGE_UUID); String image = imageUuid != null ? imageUuid.toString() : null; ri.setImage(image); addSyncAction(needsSynced, syncActions, ri, EVENT_DESTROY, checkOnly); } } }
PingInstancesMonitorImpl implements PingInstancesMonitor { void determineSyncActions(Map<String, KnownInstance> knownInstances, ReportedInstances reportedInstances, Map<String, ReportedInstance> needsSynced, Map<String, String> syncActions, boolean checkOnly) { Map<String, KnownInstance> inRancher = new HashMap<String, KnownInstance>(knownInstances); Map<String, ReportedInstance> onHost = new HashMap<String, ReportedInstance>(reportedInstances.byExternalId); for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byUuid.entrySet()) { KnownInstance ki = knownInstances.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getValue().getExternalId(), reported.getKey()); } } if (!onHost.isEmpty() || !inRancher.isEmpty()) { Map<String, KnownInstance> knownByExternalId = new HashMap<String, KnownInstance>(); for (KnownInstance ki : knownInstances.values()) { if (StringUtils.isNotEmpty(ki.getExternalId())) knownByExternalId.put(ki.getExternalId(), ki); } for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byExternalId.entrySet()) { KnownInstance ki = knownByExternalId.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getKey(), ki.getUuid()); } } } for (Map.Entry<String, ReportedInstance> create : onHost.entrySet()) { ReportedInstance ri = create.getValue(); addSyncAction(needsSynced, syncActions, ri, EVENT_START, checkOnly); } for (KnownInstance ki : inRancher.values()) { List<String> forRemove = Arrays.asList(CommonStatesConstants.REMOVING, InstanceConstants.STATE_ERROR, InstanceConstants.STATE_ERRORING); if (objectMetaDataManager.isTransitioningState(Instance.class, ki.getState()) || ki.getRemoved() != null || forRemove.contains(ki.getState()) || (STATE_STOPPED.equals(ki.getState()) && StringUtils.isEmpty(ki.getExternalId()))) continue; ReportedInstance ri = new ReportedInstance(); ri.setExternalId(ki.getExternalId()); ri.setUuid(ki.getUuid()); Object imageUuid = CollectionUtils.getNestedValue(ki.getData(), DataUtils.FIELDS, FIELD_IMAGE_UUID); String image = imageUuid != null ? imageUuid.toString() : null; ri.setImage(image); addSyncAction(needsSynced, syncActions, ri, EVENT_DESTROY, checkOnly); } }  }
PingInstancesMonitorImpl implements PingInstancesMonitor { void determineSyncActions(Map<String, KnownInstance> knownInstances, ReportedInstances reportedInstances, Map<String, ReportedInstance> needsSynced, Map<String, String> syncActions, boolean checkOnly) { Map<String, KnownInstance> inRancher = new HashMap<String, KnownInstance>(knownInstances); Map<String, ReportedInstance> onHost = new HashMap<String, ReportedInstance>(reportedInstances.byExternalId); for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byUuid.entrySet()) { KnownInstance ki = knownInstances.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getValue().getExternalId(), reported.getKey()); } } if (!onHost.isEmpty() || !inRancher.isEmpty()) { Map<String, KnownInstance> knownByExternalId = new HashMap<String, KnownInstance>(); for (KnownInstance ki : knownInstances.values()) { if (StringUtils.isNotEmpty(ki.getExternalId())) knownByExternalId.put(ki.getExternalId(), ki); } for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byExternalId.entrySet()) { KnownInstance ki = knownByExternalId.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getKey(), ki.getUuid()); } } } for (Map.Entry<String, ReportedInstance> create : onHost.entrySet()) { ReportedInstance ri = create.getValue(); addSyncAction(needsSynced, syncActions, ri, EVENT_START, checkOnly); } for (KnownInstance ki : inRancher.values()) { List<String> forRemove = Arrays.asList(CommonStatesConstants.REMOVING, InstanceConstants.STATE_ERROR, InstanceConstants.STATE_ERRORING); if (objectMetaDataManager.isTransitioningState(Instance.class, ki.getState()) || ki.getRemoved() != null || forRemove.contains(ki.getState()) || (STATE_STOPPED.equals(ki.getState()) && StringUtils.isEmpty(ki.getExternalId()))) continue; ReportedInstance ri = new ReportedInstance(); ri.setExternalId(ki.getExternalId()); ri.setUuid(ki.getUuid()); Object imageUuid = CollectionUtils.getNestedValue(ki.getData(), DataUtils.FIELDS, FIELD_IMAGE_UUID); String image = imageUuid != null ? imageUuid.toString() : null; ri.setImage(image); addSyncAction(needsSynced, syncActions, ri, EVENT_DESTROY, checkOnly); } }  @Override void pingReply(Ping ping); @Override void computeInstanceActivateReply(Event event); }
PingInstancesMonitorImpl implements PingInstancesMonitor { void determineSyncActions(Map<String, KnownInstance> knownInstances, ReportedInstances reportedInstances, Map<String, ReportedInstance> needsSynced, Map<String, String> syncActions, boolean checkOnly) { Map<String, KnownInstance> inRancher = new HashMap<String, KnownInstance>(knownInstances); Map<String, ReportedInstance> onHost = new HashMap<String, ReportedInstance>(reportedInstances.byExternalId); for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byUuid.entrySet()) { KnownInstance ki = knownInstances.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getValue().getExternalId(), reported.getKey()); } } if (!onHost.isEmpty() || !inRancher.isEmpty()) { Map<String, KnownInstance> knownByExternalId = new HashMap<String, KnownInstance>(); for (KnownInstance ki : knownInstances.values()) { if (StringUtils.isNotEmpty(ki.getExternalId())) knownByExternalId.put(ki.getExternalId(), ki); } for (Map.Entry<String, ReportedInstance> reported : reportedInstances.byExternalId.entrySet()) { KnownInstance ki = knownByExternalId.get(reported.getKey()); if (ki != null) { removeAndDetermineSyncAction(needsSynced, syncActions, checkOnly, inRancher, onHost, reported.getValue(), ki, reported.getKey(), ki.getUuid()); } } } for (Map.Entry<String, ReportedInstance> create : onHost.entrySet()) { ReportedInstance ri = create.getValue(); addSyncAction(needsSynced, syncActions, ri, EVENT_START, checkOnly); } for (KnownInstance ki : inRancher.values()) { List<String> forRemove = Arrays.asList(CommonStatesConstants.REMOVING, InstanceConstants.STATE_ERROR, InstanceConstants.STATE_ERRORING); if (objectMetaDataManager.isTransitioningState(Instance.class, ki.getState()) || ki.getRemoved() != null || forRemove.contains(ki.getState()) || (STATE_STOPPED.equals(ki.getState()) && StringUtils.isEmpty(ki.getExternalId()))) continue; ReportedInstance ri = new ReportedInstance(); ri.setExternalId(ki.getExternalId()); ri.setUuid(ki.getUuid()); Object imageUuid = CollectionUtils.getNestedValue(ki.getData(), DataUtils.FIELDS, FIELD_IMAGE_UUID); String image = imageUuid != null ? imageUuid.toString() : null; ri.setImage(image); addSyncAction(needsSynced, syncActions, ri, EVENT_DESTROY, checkOnly); } }  @Override void pingReply(Ping ping); @Override void computeInstanceActivateReply(Event event);  }