Search in sources :

Example 6 with CoordinatorStreamMessage

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

the class ConfigManager method processConfigMessages.

/**
   * This function reads all the messages with "set-config" type added to the coordinator stream since the last time the method was invoked
   *
   * @param keysToProcess a list of keys to process. Only messages with these keys will call their handler function,
   *                      and other messages will be skipped. If the list is empty all messages will be skipped.
   */
@SuppressWarnings("unchecked")
private void processConfigMessages(List<String> keysToProcess) {
    if (!coordinatorStreamConsumer.hasNewMessages(coordinatorStreamIterator)) {
        return;
    }
    if (keysToProcess == null) {
        throw new IllegalArgumentException("The keys to process list is null");
    }
    for (CoordinatorStreamMessage message : coordinatorStreamConsumer.getUnreadMessages(coordinatorStreamIterator, SetConfig.TYPE)) {
        String key = null;
        try {
            SetConfig setConfigMessage = new SetConfig(message);
            key = setConfigMessage.getKey();
            Map<String, String> valuesMap = (Map<String, String>) setConfigMessage.getMessageMap().get("values");
            String value = null;
            if (valuesMap != null) {
                value = valuesMap.get("value");
            }
            log.debug("Received set-config message with key: " + key + " and value: " + value);
            if (keysToProcess.contains(key)) {
                if (key.equals(YARN_CONTAINER_COUNT_OPT)) {
                    handleYarnContainerChange(value);
                } else if (key.equals(SERVER_URL_OPT)) {
                    handleServerURLChange(value);
                } else {
                    log.info("Setting the " + key + " configuration is currently not supported, skipping the message");
                }
            }
        //TODO: change the handlers to implement a common interface, to make them pluggable
        } catch (Exception e) {
            log.debug("Error in reading a message, skipping message with key " + key);
        }
    }
}
Also used : CoordinatorStreamMessage(org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage) SetConfig(org.apache.samza.coordinator.stream.messages.SetConfig) Map(java.util.Map) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) IOException(java.io.IOException) YarnException(org.apache.hadoop.yarn.exceptions.YarnException)

Example 7 with CoordinatorStreamMessage

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

the class TestCoordinatorStreamMessage method testCoordinatorStreamMessage.

@Test
public void testCoordinatorStreamMessage() {
    CoordinatorStreamMessage message = new CoordinatorStreamMessage("source");
    assertEquals("source", message.getSource());
    assertEquals(CoordinatorStreamMessage.VERSION, message.getVersion());
    assertNotNull(message.getUsername());
    assertTrue(message.getTimestamp() > 0);
    assertTrue(!message.isDelete());
    CoordinatorStreamMessage secondMessage = new CoordinatorStreamMessage(message.getKeyArray(), message.getMessageMap());
    assertEquals(secondMessage, message);
}
Also used : CoordinatorStreamMessage(org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage) Test(org.junit.Test)

Example 8 with CoordinatorStreamMessage

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

the class TestCoordinatorStreamSystemConsumer method testOrderKeyRewrite.

/**
   * Verify that if a particular key-value is written, then another, then the original again,
   * that the original occurs last in the set.
   */
@Test
public void testOrderKeyRewrite() throws InterruptedException {
    final SystemStream systemStream = new SystemStream("system", "stream");
    final SystemStreamPartition ssp = new SystemStreamPartition(systemStream, new Partition(0));
    final SystemConsumer systemConsumer = mock(SystemConsumer.class);
    final List<IncomingMessageEnvelope> list = new ArrayList<>();
    SetConfig setConfig1 = new SetConfig("source", "key1", "value1");
    SetConfig setConfig2 = new SetConfig("source", "key1", "value2");
    SetConfig setConfig3 = new SetConfig("source", "key1", "value1");
    list.add(createIncomingMessageEnvelope(setConfig1, ssp));
    list.add(createIncomingMessageEnvelope(setConfig2, ssp));
    list.add(createIncomingMessageEnvelope(setConfig3, ssp));
    Map<SystemStreamPartition, List<IncomingMessageEnvelope>> messages = new HashMap<SystemStreamPartition, List<IncomingMessageEnvelope>>() {

        {
            put(ssp, list);
        }
    };
    when(systemConsumer.poll(anySet(), anyLong())).thenReturn(messages, Collections.<SystemStreamPartition, List<IncomingMessageEnvelope>>emptyMap());
    CoordinatorStreamSystemConsumer consumer = new CoordinatorStreamSystemConsumer(systemStream, systemConsumer, new SinglePartitionWithoutOffsetsSystemAdmin());
    consumer.bootstrap();
    Set<CoordinatorStreamMessage> bootstrappedMessages = consumer.getBoostrappedStream();
    // First message should have been removed as a duplicate
    assertEquals(2, bootstrappedMessages.size());
    CoordinatorStreamMessage[] coordinatorStreamMessages = bootstrappedMessages.toArray(new CoordinatorStreamMessage[2]);
    assertEquals(setConfig2, coordinatorStreamMessages[0]);
    //Config 3 MUST be the last message, not config 2
    assertEquals(setConfig3, coordinatorStreamMessages[1]);
}
Also used : SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Partition(org.apache.samza.Partition) SystemConsumer(org.apache.samza.system.SystemConsumer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SystemStream(org.apache.samza.system.SystemStream) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) ArrayList(java.util.ArrayList) CoordinatorStreamMessage(org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage) SetConfig(org.apache.samza.coordinator.stream.messages.SetConfig) SinglePartitionWithoutOffsetsSystemAdmin(org.apache.samza.util.SinglePartitionWithoutOffsetsSystemAdmin) ArrayList(java.util.ArrayList) List(java.util.List) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Example 9 with CoordinatorStreamMessage

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

the class TestCoordinatorStreamSystemProducer method testCoordinatorStreamSystemProducer.

@Test
public void testCoordinatorStreamSystemProducer() {
    String source = "source";
    SystemStream systemStream = new SystemStream("system", "stream");
    MockCoordinatorSystemProducer systemProducer = new MockCoordinatorSystemProducer(source);
    MockSystemAdmin systemAdmin = new MockSystemAdmin();
    CoordinatorStreamSystemProducer producer = new CoordinatorStreamSystemProducer(systemStream, systemProducer, systemAdmin);
    SetConfig setConfig1 = new SetConfig(source, "job.name", "my-job-name");
    SetConfig setConfig2 = new SetConfig(source, "job.id", "1234");
    Delete delete = new Delete(source, "job.name", SetConfig.TYPE);
    assertFalse(systemProducer.isRegistered());
    producer.register(source);
    assertTrue(systemProducer.isRegistered());
    assertFalse(systemProducer.isStarted());
    producer.start();
    assertTrue(systemProducer.isStarted());
    producer.send(setConfig1);
    producer.send(setConfig2);
    producer.send(delete);
    assertFalse(systemProducer.isStopped());
    producer.stop();
    assertTrue(systemProducer.isStopped());
    List<OutgoingMessageEnvelope> envelopes = systemProducer.getEnvelopes();
    OutgoingMessageEnvelope envelope0 = envelopes.get(0);
    OutgoingMessageEnvelope envelope1 = envelopes.get(1);
    OutgoingMessageEnvelope envelope2 = envelopes.get(2);
    TypeReference<Object[]> keyRef = new TypeReference<Object[]>() {
    };
    TypeReference<Map<String, Object>> msgRef = new TypeReference<Map<String, Object>>() {
    };
    assertEquals(3, envelopes.size());
    assertEquals(new CoordinatorStreamMessage(setConfig1), new CoordinatorStreamMessage(deserialize((byte[]) envelope0.getKey(), keyRef), deserialize((byte[]) envelope0.getMessage(), msgRef)));
    assertEquals(new CoordinatorStreamMessage(setConfig2), new CoordinatorStreamMessage(deserialize((byte[]) envelope1.getKey(), keyRef), deserialize((byte[]) envelope1.getMessage(), msgRef)));
    assertEquals(new CoordinatorStreamMessage(delete), new CoordinatorStreamMessage(deserialize((byte[]) envelope2.getKey(), keyRef), deserialize((byte[]) envelope2.getMessage(), msgRef)));
}
Also used : Delete(org.apache.samza.coordinator.stream.messages.Delete) SystemStream(org.apache.samza.system.SystemStream) CoordinatorStreamMessage(org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage) SetConfig(org.apache.samza.coordinator.stream.messages.SetConfig) TypeReference(org.codehaus.jackson.type.TypeReference) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Map(java.util.Map) Test(org.junit.Test)

Example 10 with CoordinatorStreamMessage

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

the class TestCoordinatorStreamMessage method testCoordinatorStreamMessageIsDelete.

@Test
public void testCoordinatorStreamMessageIsDelete() {
    CoordinatorStreamMessage message = new CoordinatorStreamMessage(new Object[] {}, null);
    assertTrue(message.isDelete());
    assertNull(message.getMessageMap());
}
Also used : CoordinatorStreamMessage(org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage) Test(org.junit.Test)

Aggregations

CoordinatorStreamMessage (org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage)12 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 SetConfig (org.apache.samza.coordinator.stream.messages.SetConfig)4 LinkedHashSet (java.util.LinkedHashSet)3 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)3 SetContainerHostMapping (org.apache.samza.coordinator.stream.messages.SetContainerHostMapping)2 SystemStream (org.apache.samza.system.SystemStream)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 YarnException (org.apache.hadoop.yarn.exceptions.YarnException)1 Partition (org.apache.samza.Partition)1 SamzaException (org.apache.samza.SamzaException)1 TaskName (org.apache.samza.container.TaskName)1 MockCoordinatorStreamSystemProducer (org.apache.samza.coordinator.stream.MockCoordinatorStreamSystemFactory.MockCoordinatorStreamSystemProducer)1 Delete (org.apache.samza.coordinator.stream.messages.Delete)1 SetChangelogMapping (org.apache.samza.coordinator.stream.messages.SetChangelogMapping)1