Search in sources :

Example 16 with MetricsRegistry

use of org.apache.samza.metrics.MetricsRegistry in project samza by apache.

the class TestCachingTable method initTables.

private void initTables(boolean isTimerMetricsDisabled, ReadWriteUpdateTable... tables) {
    Map<String, String> config = new HashMap<>();
    if (isTimerMetricsDisabled) {
        config.put(MetricsConfig.METRICS_TIMER_ENABLED, "false");
    }
    Context context = new MockContext();
    doReturn(new MapConfig(config)).when(context.getJobContext()).getConfig();
    metricsRegistry = mock(MetricsRegistry.class);
    doReturn(mock(Timer.class)).when(metricsRegistry).newTimer(anyString(), anyString());
    doReturn(mock(Counter.class)).when(metricsRegistry).newCounter(anyString(), anyString());
    doReturn(mock(Gauge.class)).when(metricsRegistry).newGauge(anyString(), any());
    doReturn(metricsRegistry).when(context.getContainerContext()).getContainerMetricsRegistry();
    Arrays.asList(tables).forEach(t -> t.init(context));
}
Also used : MockContext(org.apache.samza.context.MockContext) Context(org.apache.samza.context.Context) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) MockContext(org.apache.samza.context.MockContext) Counter(org.apache.samza.metrics.Counter) Timer(org.apache.samza.metrics.Timer) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Matchers.anyString(org.mockito.Matchers.anyString) MapConfig(org.apache.samza.config.MapConfig) Gauge(org.apache.samza.metrics.Gauge)

Example 17 with MetricsRegistry

use of org.apache.samza.metrics.MetricsRegistry in project samza by apache.

the class MockSystemFactory method getProducer.

public SystemProducer getProducer(String systemName, Config config, MetricsRegistry registry) {
    return new SystemProducer() {

        private final Random seed = new Random(System.currentTimeMillis());

        @Override
        public void start() {
        }

        @Override
        public void stop() {
        }

        @Override
        public void register(String source) {
        }

        @Override
        public void send(String source, OutgoingMessageEnvelope envelope) {
            SystemStream systemStream = envelope.getSystemStream();
            List<SystemStreamPartition> sspForSystem = MSG_QUEUES.keySet().stream().filter(ssp -> ssp.getSystemStream().equals(systemStream)).collect(ArrayList::new, (l, ssp) -> l.add(ssp), (l1, l2) -> l1.addAll(l2));
            if (sspForSystem.isEmpty()) {
                MSG_QUEUES.putIfAbsent(new SystemStreamPartition(systemStream, new Partition(0)), new ArrayList<>());
                sspForSystem.add(new SystemStreamPartition(systemStream, new Partition(0)));
            }
            int partitionCount = sspForSystem.size();
            int partitionId = envelope.getPartitionKey() == null ? envelope.getKey() == null ? this.seed.nextInt(partitionCount) : envelope.getKey().hashCode() % partitionCount : envelope.getPartitionKey().hashCode() % partitionCount;
            SystemStreamPartition ssp = new SystemStreamPartition(envelope.getSystemStream(), new Partition(partitionId));
            List<IncomingMessageEnvelope> msgQueue = MSG_QUEUES.get(ssp);
            msgQueue.add(new IncomingMessageEnvelope(ssp, null, envelope.getKey(), envelope.getMessage()));
        }

        @Override
        public void flush(String source) {
        }
    };
}
Also used : HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Partition(org.apache.samza.Partition) Set(java.util.Set) HashMap(java.util.HashMap) Random(java.util.Random) Config(org.apache.samza.config.Config) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) ArrayList(java.util.ArrayList) Partition(org.apache.samza.Partition) ArrayList(java.util.ArrayList) Random(java.util.Random)

Example 18 with MetricsRegistry

use of org.apache.samza.metrics.MetricsRegistry in project samza by apache.

the class OperatorImpl method init.

/**
 * Initialize this {@link OperatorImpl} and its user-defined functions.
 *
 * @param internalTaskContext the {@link InternalTaskContext} for the task
 */
public final void init(InternalTaskContext internalTaskContext) {
    final Context context = internalTaskContext.getContext();
    String opId = getOpImplId();
    if (initialized) {
        throw new IllegalStateException(String.format("Attempted to initialize Operator %s more than once.", opId));
    }
    if (closed) {
        throw new IllegalStateException(String.format("Attempted to initialize Operator %s after it was closed.", opId));
    }
    this.highResClock = createHighResClock(context.getJobContext().getConfig());
    registeredOperators = new LinkedHashSet<>();
    prevOperators = new LinkedHashSet<>();
    inputStreams = new LinkedHashSet<>();
    final ContainerContext containerContext = context.getContainerContext();
    final MetricsRegistry metricsRegistry = containerContext.getContainerMetricsRegistry();
    this.numMessage = metricsRegistry.newCounter(METRICS_GROUP, opId + "-messages");
    this.handleMessageNs = metricsRegistry.newTimer(METRICS_GROUP, opId + "-handle-message-ns");
    this.handleTimerNs = metricsRegistry.newTimer(METRICS_GROUP, opId + "-handle-timer-ns");
    final TaskContext taskContext = context.getTaskContext();
    this.taskName = taskContext.getTaskModel().getTaskName();
    this.eosStates = (EndOfStreamStates) internalTaskContext.fetchObject(EndOfStreamStates.class.getName());
    this.watermarkStates = (WatermarkStates) internalTaskContext.fetchObject(WatermarkStates.class.getName());
    this.controlMessageSender = new ControlMessageSender(internalTaskContext.getStreamMetadataCache());
    this.taskModel = taskContext.getTaskModel();
    this.callbackScheduler = taskContext.getCallbackScheduler();
    handleInit(context);
    this.elasticityFactor = new JobConfig(context.getJobContext().getConfig()).getElasticityFactor();
    initialized = true;
}
Also used : TaskContext(org.apache.samza.context.TaskContext) ContainerContext(org.apache.samza.context.ContainerContext) Context(org.apache.samza.context.Context) InternalTaskContext(org.apache.samza.context.InternalTaskContext) ContainerContext(org.apache.samza.context.ContainerContext) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) TaskContext(org.apache.samza.context.TaskContext) InternalTaskContext(org.apache.samza.context.InternalTaskContext) JobConfig(org.apache.samza.config.JobConfig)

Aggregations

MetricsRegistry (org.apache.samza.metrics.MetricsRegistry)18 HashMap (java.util.HashMap)7 Counter (org.apache.samza.metrics.Counter)7 Timer (org.apache.samza.metrics.Timer)7 MapConfig (org.apache.samza.config.MapConfig)6 Map (java.util.Map)5 Config (org.apache.samza.config.Config)5 Context (org.apache.samza.context.Context)5 TaskModel (org.apache.samza.job.model.TaskModel)4 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)4 Before (org.junit.Before)4 File (java.io.File)3 Set (java.util.Set)3 SamzaException (org.apache.samza.SamzaException)3 TaskName (org.apache.samza.container.TaskName)3 ContainerContext (org.apache.samza.context.ContainerContext)3 ContainerModel (org.apache.samza.job.model.ContainerModel)3 JobModel (org.apache.samza.job.model.JobModel)3 Gauge (org.apache.samza.metrics.Gauge)3 Test (org.junit.Test)3