Search in sources :

Example 1 with SetContainerHostMapping

use of org.apache.samza.coordinator.stream.messages.SetContainerHostMapping in project samza by apache.

the class LocalityManager method readContainerLocality.

/**
   * Method to allow read container locality information from coordinator stream. This method is used
   * in {@link org.apache.samza.coordinator.JobModelManager}.
   *
   * @return the map of containerId: (hostname, jmxAddress, jmxTunnelAddress)
   */
public Map<String, Map<String, String>> readContainerLocality() {
    if (this.writeOnly) {
        throw new UnsupportedOperationException("Read container locality function is not supported in write-only LocalityManager");
    }
    Map<String, Map<String, String>> allMappings = new HashMap<>();
    for (CoordinatorStreamMessage message : getBootstrappedStream(SetContainerHostMapping.TYPE)) {
        SetContainerHostMapping mapping = new SetContainerHostMapping(message);
        Map<String, String> localityMappings = new HashMap<>();
        localityMappings.put(SetContainerHostMapping.HOST_KEY, mapping.getHostLocality());
        localityMappings.put(SetContainerHostMapping.JMX_URL_KEY, mapping.getJmxUrl());
        localityMappings.put(SetContainerHostMapping.JMX_TUNNELING_URL_KEY, mapping.getJmxTunnelingUrl());
        allMappings.put(mapping.getKey(), localityMappings);
    }
    containerToHostMapping = Collections.unmodifiableMap(allMappings);
    for (Map.Entry<String, Map<String, String>> entry : containerToHostMapping.entrySet()) {
        log.debug(String.format("Locality for container %s: %s", entry.getKey(), entry.getValue()));
    }
    return allMappings;
}
Also used : CoordinatorStreamMessage(org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage) HashMap(java.util.HashMap) SetContainerHostMapping(org.apache.samza.coordinator.stream.messages.SetContainerHostMapping) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with SetContainerHostMapping

use of org.apache.samza.coordinator.stream.messages.SetContainerHostMapping in project samza by apache.

the class TestLocalityManager method testWriteOnlyLocalityManager.

@Test
public void testWriteOnlyLocalityManager() {
    MockCoordinatorStreamSystemProducer producer = mockCoordinatorStreamSystemFactory.getCoordinatorStreamSystemProducer(config, null);
    LocalityManager localityManager = new LocalityManager(producer);
    localityManager.register("containerId-1");
    assertTrue(producer.isRegistered());
    assertEquals(producer.getRegisteredSource(), "SamzaContainer-containerId-1");
    localityManager.start();
    assertTrue(producer.isStarted());
    localityManager.writeContainerToHostMapping("1", "localhost", "jmx:localhost:8181", "jmx:tunnel:localhost:9191");
    try {
        localityManager.readContainerLocality();
        fail("Should have thrown UnsupportedOperationException");
    } catch (UnsupportedOperationException uoe) {
    // expected
    }
    assertEquals(producer.getEnvelopes().size(), 1);
    CoordinatorStreamMessage coordinatorStreamMessage = MockCoordinatorStreamSystemFactory.deserializeCoordinatorStreamMessage(producer.getEnvelopes().get(0));
    SetContainerHostMapping expectedContainerMap = new SetContainerHostMapping("SamzaContainer-1", "1", "localhost", "jmx:localhost:8181", "jmx:tunnel:localhost:9191");
    assertEquals(expectedContainerMap, coordinatorStreamMessage);
    localityManager.stop();
    assertTrue(producer.isStopped());
}
Also used : CoordinatorStreamMessage(org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage) MockCoordinatorStreamSystemProducer(org.apache.samza.coordinator.stream.MockCoordinatorStreamSystemFactory.MockCoordinatorStreamSystemProducer) SetContainerHostMapping(org.apache.samza.coordinator.stream.messages.SetContainerHostMapping) Test(org.junit.Test)

Example 3 with SetContainerHostMapping

use of org.apache.samza.coordinator.stream.messages.SetContainerHostMapping in project samza by apache.

the class LocalityManager method writeContainerToHostMapping.

/**
   * Method to write locality info to coordinator stream. This method is used in {@link SamzaContainer}.
   *
   * @param containerId  the {@link SamzaContainer} ID
   * @param hostName  the hostname
   * @param jmxAddress  the JMX URL address
   * @param jmxTunnelingAddress  the JMX Tunnel URL address
   */
public void writeContainerToHostMapping(String containerId, String hostName, String jmxAddress, String jmxTunnelingAddress) {
    Map<String, String> existingMappings = containerToHostMapping.get(containerId);
    String existingHostMapping = existingMappings != null ? existingMappings.get(SetContainerHostMapping.HOST_KEY) : null;
    if (existingHostMapping != null && !existingHostMapping.equals(hostName)) {
        log.info("Container {} moved from {} to {}", new Object[] { containerId, existingHostMapping, hostName });
    } else {
        log.info("Container {} started at {}", containerId, hostName);
    }
    send(new SetContainerHostMapping(getSource() + containerId, String.valueOf(containerId), hostName, jmxAddress, jmxTunnelingAddress));
    Map<String, String> mappings = new HashMap<>();
    mappings.put(SetContainerHostMapping.HOST_KEY, hostName);
    mappings.put(SetContainerHostMapping.JMX_URL_KEY, jmxAddress);
    mappings.put(SetContainerHostMapping.JMX_TUNNELING_URL_KEY, jmxTunnelingAddress);
    containerToHostMapping.put(containerId, mappings);
}
Also used : HashMap(java.util.HashMap) SetContainerHostMapping(org.apache.samza.coordinator.stream.messages.SetContainerHostMapping)

Aggregations

SetContainerHostMapping (org.apache.samza.coordinator.stream.messages.SetContainerHostMapping)3 HashMap (java.util.HashMap)2 CoordinatorStreamMessage (org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage)2 Map (java.util.Map)1 MockCoordinatorStreamSystemProducer (org.apache.samza.coordinator.stream.MockCoordinatorStreamSystemFactory.MockCoordinatorStreamSystemProducer)1 Test (org.junit.Test)1