Search in sources :

Example 6 with SystemProducer

use of org.apache.samza.system.SystemProducer in project beam by apache.

the class TranslationContext method createDummyStreamDescriptor.

/**
 * The dummy stream created will only be used in Beam tests.
 */
private static InputDescriptor<OpMessage<String>, ?> createDummyStreamDescriptor(String id) {
    final GenericSystemDescriptor dummySystem = new GenericSystemDescriptor(id, InMemorySystemFactory.class.getName());
    final GenericInputDescriptor<OpMessage<String>> dummyInput = dummySystem.getInputDescriptor(id, new NoOpSerde<>());
    dummyInput.withOffsetDefault(SystemStreamMetadata.OffsetType.OLDEST);
    final Config config = new MapConfig(dummyInput.toConfig(), dummySystem.toConfig());
    final SystemFactory factory = new InMemorySystemFactory();
    final StreamSpec dummyStreamSpec = new StreamSpec(id, id, id, 1);
    factory.getAdmin(id, config).createStream(dummyStreamSpec);
    final SystemProducer producer = factory.getProducer(id, config, null);
    final SystemStream sysStream = new SystemStream(id, id);
    final Consumer<Object> sendFn = (msg) -> {
        producer.send(id, new OutgoingMessageEnvelope(sysStream, 0, null, msg));
    };
    final WindowedValue<String> windowedValue = WindowedValue.timestampedValueInGlobalWindow("dummy", new Instant());
    sendFn.accept(OpMessage.ofElement(windowedValue));
    sendFn.accept(new WatermarkMessage(BoundedWindow.TIMESTAMP_MAX_VALUE.getMillis()));
    sendFn.accept(new EndOfStreamMessage(null));
    return dummyInput;
}
Also used : InMemorySystemFactory(org.apache.samza.system.inmemory.InMemorySystemFactory) WindowedValue(org.apache.beam.sdk.util.WindowedValue) TableDescriptor(org.apache.samza.table.descriptors.TableDescriptor) GenericSystemDescriptor(org.apache.samza.system.descriptors.GenericSystemDescriptor) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) OpMessage(org.apache.beam.runners.samza.runtime.OpMessage) GenericInputDescriptor(org.apache.samza.system.descriptors.GenericInputDescriptor) TransformInputs(org.apache.beam.runners.core.construction.TransformInputs) SystemStreamMetadata(org.apache.samza.system.SystemStreamMetadata) PTransform(org.apache.beam.sdk.transforms.PTransform) HashSet(java.util.HashSet) TupleTag(org.apache.beam.sdk.values.TupleTag) SystemStream(org.apache.samza.system.SystemStream) Map(java.util.Map) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) WatermarkMessage(org.apache.samza.system.WatermarkMessage) MapConfig(org.apache.samza.config.MapConfig) KV(org.apache.samza.operators.KV) NoOpSerde(org.apache.samza.serializers.NoOpSerde) AppliedPTransform(org.apache.beam.sdk.runners.AppliedPTransform) OutputDescriptor(org.apache.samza.system.descriptors.OutputDescriptor) MessageStream(org.apache.samza.operators.MessageStream) Table(org.apache.samza.table.Table) InputDescriptor(org.apache.samza.system.descriptors.InputDescriptor) Logger(org.slf4j.Logger) Set(java.util.Set) SystemFactory(org.apache.samza.system.SystemFactory) StreamSpec(org.apache.samza.system.StreamSpec) UUID(java.util.UUID) PCollection(org.apache.beam.sdk.values.PCollection) HashIdGenerator(org.apache.beam.runners.samza.util.HashIdGenerator) Consumer(java.util.function.Consumer) SamzaPipelineOptions(org.apache.beam.runners.samza.SamzaPipelineOptions) List(java.util.List) PValue(org.apache.beam.sdk.values.PValue) SystemProducer(org.apache.samza.system.SystemProducer) StreamApplicationDescriptor(org.apache.samza.application.descriptors.StreamApplicationDescriptor) PCollectionView(org.apache.beam.sdk.values.PCollectionView) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) Instant(org.joda.time.Instant) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) Config(org.apache.samza.config.Config) Collections(java.util.Collections) OutputStream(org.apache.samza.operators.OutputStream) StreamSpec(org.apache.samza.system.StreamSpec) InMemorySystemFactory(org.apache.samza.system.inmemory.InMemorySystemFactory) SystemFactory(org.apache.samza.system.SystemFactory) OpMessage(org.apache.beam.runners.samza.runtime.OpMessage) MapConfig(org.apache.samza.config.MapConfig) Config(org.apache.samza.config.Config) SystemProducer(org.apache.samza.system.SystemProducer) SystemStream(org.apache.samza.system.SystemStream) Instant(org.joda.time.Instant) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) WatermarkMessage(org.apache.samza.system.WatermarkMessage) MapConfig(org.apache.samza.config.MapConfig) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) GenericSystemDescriptor(org.apache.samza.system.descriptors.GenericSystemDescriptor) InMemorySystemFactory(org.apache.samza.system.inmemory.InMemorySystemFactory)

Example 7 with SystemProducer

use of org.apache.samza.system.SystemProducer in project samza by apache.

the class AsyncSystemProducer method flush.

/**
 * Default implementation of the flush that just waits for all the pendingFutures to be complete.
 * SystemProducer should override this, if the underlying system provides flush semantics.
 * @param source String representing the source of the message.
 */
@Override
public synchronized void flush(String source) {
    long incompleteSends = pendingFutures.stream().filter(x -> !x.isDone()).count();
    LOG.info("Trying to flush pending {} sends.", incompleteSends);
    checkForSendCallbackErrors("Received exception on message send.");
    CompletableFuture<Void> future = CompletableFuture.allOf(pendingFutures.toArray(new CompletableFuture[pendingFutures.size()]));
    try {
        // Block until all the pending sends are complete or timeout.
        future.get(DEFAULT_FLUSH_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        incompleteSends = pendingFutures.stream().filter(x -> !x.isDone()).count();
        String msg = String.format("Flush failed with error. Total pending sends %d", incompleteSends);
        LOG.error(msg, e);
        throw new SamzaException(msg, e);
    }
    pendingFutures.clear();
    checkForSendCallbackErrors("Sending one or more of the messages failed during flush.");
}
Also used : Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) SamzaException(org.apache.samza.SamzaException) StreamConfig(org.apache.samza.config.StreamConfig) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Counter(org.apache.samza.metrics.Counter) SystemProducer(org.apache.samza.system.SystemProducer) Duration(java.time.Duration) Map(java.util.Map) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) SamzaHistogram(org.apache.samza.metrics.SamzaHistogram) EventHubConfig(org.apache.samza.system.eventhub.EventHubConfig) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException) SamzaException(org.apache.samza.SamzaException) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with SystemProducer

use of org.apache.samza.system.SystemProducer in project samza by apache.

the class SystemProducerBench method start.

public void start() throws IOException, InterruptedException {
    super.start();
    String source = "SystemProducerBench";
    int size = Integer.parseInt(cmd.getOptionValue(OPT_SHORT_MESSAGE_SIZE));
    RandomValueGenerator randGenerator = new RandomValueGenerator(System.currentTimeMillis());
    value = randGenerator.getNextString(size, size).getBytes();
    NoOpMetricsRegistry metricsRegistry = new NoOpMetricsRegistry();
    List<SystemStreamPartition> ssps = createSSPs(systemName, physicalStreamName, startPartition, endPartition);
    SystemProducer producer = factory.getProducer(systemName, config, metricsRegistry);
    producer.register(source);
    producer.start();
    System.out.println("starting production at " + Instant.now());
    Instant startTime = Instant.now();
    for (int index = 0; index < totalEvents; index++) {
        SystemStreamPartition ssp = ssps.get(index % ssps.size());
        OutgoingMessageEnvelope messageEnvelope = createMessageEnvelope(ssp, index);
        producer.send(source, messageEnvelope);
    }
    System.out.println("Ending production at " + Instant.now());
    System.out.println(String.format("Event Rate is %s Messages/Sec", totalEvents * 1000 / Duration.between(startTime, Instant.now()).toMillis()));
    producer.flush(source);
    System.out.println("Ending flush at " + Instant.now());
    System.out.println(String.format("Event Rate with flush is %s Messages/Sec", totalEvents * 1000 / Duration.between(startTime, Instant.now()).toMillis()));
    producer.stop();
    System.exit(0);
}
Also used : NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) RandomValueGenerator(org.apache.samza.tools.RandomValueGenerator) SystemProducer(org.apache.samza.system.SystemProducer) Instant(java.time.Instant) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition)

Example 9 with SystemProducer

use of org.apache.samza.system.SystemProducer in project samza by apache.

the class TestInMemorySystem method testNullMessageWithValidMessageKey.

@Test
public void testNullMessageWithValidMessageKey() {
    final String messageKey = "validKey";
    SystemProducer systemProducer = systemFactory.getProducer(SYSTEM_NAME, config, mockRegistry);
    systemProducer.send(SOURCE, new OutgoingMessageEnvelope(SYSTEM_STREAM, messageKey, null));
    SystemConsumer consumer = systemFactory.getConsumer(SYSTEM_NAME, config, mockRegistry);
    Set<SystemStreamPartition> sspsToPoll = IntStream.range(0, PARTITION_COUNT).mapToObj(partition -> new SystemStreamPartition(SYSTEM_STREAM, new Partition(partition))).collect(Collectors.toSet());
    // register the consumer for ssps
    for (SystemStreamPartition ssp : sspsToPoll) {
        consumer.register(ssp, "0");
    }
    List<IncomingMessageEnvelope> results = consumeRawMessages(consumer, sspsToPoll);
    assertEquals(1, results.size());
    assertEquals(results.get(0).getKey(), messageKey);
    assertNull(results.get(0).getMessage());
}
Also used : IntStream(java.util.stream.IntStream) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Partition(org.apache.samza.Partition) Set(java.util.Set) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) SystemConsumer(org.apache.samza.system.SystemConsumer) SystemProducer(org.apache.samza.system.SystemProducer) SystemStream(org.apache.samza.system.SystemStream) Map(java.util.Map) SystemAdmin(org.apache.samza.system.SystemAdmin) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) MapConfig(org.apache.samza.config.MapConfig) SystemConsumer(org.apache.samza.system.SystemConsumer) Partition(org.apache.samza.Partition) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) SystemProducer(org.apache.samza.system.SystemProducer) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Example 10 with SystemProducer

use of org.apache.samza.system.SystemProducer in project samza by apache.

the class TestInMemorySystem method testNullMessageWithNullKey.

@Test(expected = NullPointerException.class)
public void testNullMessageWithNullKey() {
    SystemProducer systemProducer = systemFactory.getProducer(SYSTEM_NAME, config, mockRegistry);
    systemProducer.send(SOURCE, new OutgoingMessageEnvelope(SYSTEM_STREAM, null));
}
Also used : SystemProducer(org.apache.samza.system.SystemProducer) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Test(org.junit.Test)

Aggregations

SystemProducer (org.apache.samza.system.SystemProducer)14 OutgoingMessageEnvelope (org.apache.samza.system.OutgoingMessageEnvelope)7 Test (org.junit.Test)7 Duration (java.time.Duration)4 Config (org.apache.samza.config.Config)4 SystemFactory (org.apache.samza.system.SystemFactory)4 SystemStream (org.apache.samza.system.SystemStream)4 List (java.util.List)3 Map (java.util.Map)3 Set (java.util.Set)3 SamzaException (org.apache.samza.SamzaException)3 MapConfig (org.apache.samza.config.MapConfig)3 SystemConfig (org.apache.samza.config.SystemConfig)3 HashMap (java.util.HashMap)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 JobConfig (org.apache.samza.config.JobConfig)2 MetricsConfig (org.apache.samza.config.MetricsConfig)2 DiagnosticsManager (org.apache.samza.diagnostics.DiagnosticsManager)2 MetricsRegistry (org.apache.samza.metrics.MetricsRegistry)2