Search in sources :

Example 11 with MantisMasterClientApi

use of io.mantisrx.server.master.client.MantisMasterClientApi in project mantis by Netflix.

the class WorkerMetricHandlerTest method testDropDataMetricTriggersAutoScale.

@Test
public void testDropDataMetricTriggersAutoScale() throws InterruptedException {
    final String jobId = "test-job-1";
    final int stage = 1;
    final int workerIdx = 0;
    final int workerNum = 1;
    final int dropCount = 1;
    final int onNextCount = 9;
    final double dropPercent = dropCount * 100.0 / (dropCount + onNextCount);
    final List<GaugeMeasurement> gauges = Arrays.asList(new GaugeMeasurement(MetricStringConstants.ON_NEXT_COUNT, onNextCount), new GaugeMeasurement(MetricStringConstants.DROP_COUNT, dropCount));
    final MantisMasterClientApi mockMasterClientApi = mock(MantisMasterClientApi.class);
    final Map<Integer, WorkerAssignments> assignmentsMap = new HashMap<>();
    assignmentsMap.put(stage, new WorkerAssignments(stage, 1, Collections.singletonMap(1, new WorkerHost("localhost", workerIdx, Arrays.asList(31300), MantisJobState.Started, workerNum, 31301, -1))));
    when(mockMasterClientApi.schedulingChanges(jobId)).thenReturn(Observable.just(new JobSchedulingInfo(jobId, assignmentsMap)));
    final CountDownLatch latch = new CountDownLatch(1);
    final AutoScaleMetricsConfig aggregationConfig = new AutoScaleMetricsConfig();
    final WorkerMetricHandler workerMetricHandler = new WorkerMetricHandler(jobId, new Observer<JobAutoScaler.Event>() {

        @Override
        public void onCompleted() {
            logger.warn("onCompleted");
        }

        @Override
        public void onError(Throwable e) {
            logger.warn("onError {}", e.getMessage(), e);
        }

        @Override
        public void onNext(JobAutoScaler.Event event) {
            logger.info("got auto scale event {}", event);
            JobAutoScaler.Event expected = new JobAutoScaler.Event(StageScalingPolicy.ScalingReason.DataDrop, stage, dropPercent, 1, "");
            assertEquals(expected, event);
            latch.countDown();
        }
    }, mockMasterClientApi, aggregationConfig);
    final Observer<MetricData> metricDataObserver = workerMetricHandler.initAndGetMetricDataObserver();
    // Purposely create a new String for jobId
    metricDataObserver.onNext(new MetricData(new String(jobId), stage, workerIdx, workerNum, DATA_DROP_METRIC_GROUP, gauges));
    assertTrue(latch.await(30 + 5, /* leeway */
    TimeUnit.SECONDS));
}
Also used : WorkerHost(io.mantisrx.server.core.WorkerHost) HashMap(java.util.HashMap) JobSchedulingInfo(io.mantisrx.server.core.JobSchedulingInfo) Matchers.anyString(org.mockito.Matchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) MantisMasterClientApi(io.mantisrx.server.master.client.MantisMasterClientApi) WorkerAssignments(io.mantisrx.server.core.WorkerAssignments) GaugeMeasurement(io.mantisrx.common.metrics.measurement.GaugeMeasurement) Test(org.junit.Test)

Example 12 with MantisMasterClientApi

use of io.mantisrx.server.master.client.MantisMasterClientApi in project mantis by Netflix.

the class WorkerMetricHandlerTest method testOutlierResubmitWorks.

@Test
public void testOutlierResubmitWorks() throws InterruptedException {
    final String jobId = "test-job-1";
    final int stage = 1;
    final int workerIdx = 0;
    final int workerNum = 1;
    final int numWorkers = 3;
    final int dropCount = 1;
    final int onNextCount = 9;
    final double dropPercent = dropCount * 100.0 / (dropCount + onNextCount);
    final List<GaugeMeasurement> outlierDropGauges = Arrays.asList(new GaugeMeasurement(MetricStringConstants.ON_NEXT_COUNT, onNextCount), new GaugeMeasurement(MetricStringConstants.DROP_COUNT, dropCount));
    final List<GaugeMeasurement> zeroDropGauges = Arrays.asList(new GaugeMeasurement(MetricStringConstants.ON_NEXT_COUNT, onNextCount), new GaugeMeasurement(MetricStringConstants.DROP_COUNT, 0));
    final MantisMasterClientApi mockMasterClientApi = mock(MantisMasterClientApi.class);
    final Map<Integer, WorkerAssignments> assignmentsMap = new HashMap<>();
    Map<Integer, WorkerHost> hosts = new HashMap<>();
    hosts.put(workerNum, new WorkerHost("localhost", workerIdx, Arrays.asList(31300), MantisJobState.Started, workerNum, 31301, -1));
    hosts.put(workerNum + 1, new WorkerHost("localhost", workerIdx + 1, Arrays.asList(31302), MantisJobState.Started, workerNum, 31303, -1));
    hosts.put(workerNum + 2, new WorkerHost("localhost", workerIdx + 2, Arrays.asList(31304), MantisJobState.Started, workerNum, 31305, -1));
    assignmentsMap.put(stage, new WorkerAssignments(stage, numWorkers, hosts));
    final CountDownLatch resubmitLatch = new CountDownLatch(1);
    final CountDownLatch autoScaleLatch = new CountDownLatch(1);
    when(mockMasterClientApi.schedulingChanges(jobId)).thenReturn(Observable.just(new JobSchedulingInfo(jobId, assignmentsMap)));
    when(mockMasterClientApi.resubmitJobWorker(anyString(), anyString(), anyInt(), anyString())).thenAnswer(new Answer<Observable<Boolean>>() {

        @Override
        public Observable<Boolean> answer(InvocationOnMock invocation) throws Throwable {
            final Object[] arguments = invocation.getArguments();
            final String jobIdRecv = (String) arguments[0];
            final String user = (String) arguments[1];
            final int resubmittedWorkerNum = (Integer) arguments[2];
            // final String reason = (String)arguments[3];
            final Observable<Boolean> result = Observable.just(1).map(new Func1<Integer, Boolean>() {

                @Override
                public Boolean call(Integer integer) {
                    logger.info("resubmitting worker {} of jobId {}", resubmittedWorkerNum, jobId);
                    assertEquals(workerNum, resubmittedWorkerNum);
                    assertEquals(user, "JobMaster");
                    assertEquals(jobId, jobIdRecv);
                    resubmitLatch.countDown();
                    return true;
                }
            });
            return result;
        }
    });
    final AutoScaleMetricsConfig aggregationConfig = new AutoScaleMetricsConfig();
    final WorkerMetricHandler workerMetricHandler = new WorkerMetricHandler(jobId, new Observer<JobAutoScaler.Event>() {

        @Override
        public void onCompleted() {
            logger.warn("onCompleted");
        }

        @Override
        public void onError(Throwable e) {
            logger.warn("onError {}", e.getMessage(), e);
        }

        @Override
        public void onNext(JobAutoScaler.Event event) {
            logger.info("got auto scale event {}", event);
            JobAutoScaler.Event expected = new JobAutoScaler.Event(StageScalingPolicy.ScalingReason.DataDrop, stage, dropPercent / numWorkers, numWorkers, "");
            assertEquals(expected, event);
            autoScaleLatch.countDown();
        }
    }, mockMasterClientApi, aggregationConfig);
    final Observer<MetricData> metricDataObserver = workerMetricHandler.initAndGetMetricDataObserver();
    final int minDataPointsForOutlierTrigger = 16;
    for (int i = 0; i <= minDataPointsForOutlierTrigger; i++) {
        metricDataObserver.onNext(new MetricData(jobId, stage, workerIdx, workerNum, DATA_DROP_METRIC_GROUP, outlierDropGauges));
        metricDataObserver.onNext(new MetricData(jobId, stage, workerIdx + 1, workerNum + 1, DATA_DROP_METRIC_GROUP, zeroDropGauges));
        metricDataObserver.onNext(new MetricData(jobId, stage, workerIdx + 2, workerNum + 2, DATA_DROP_METRIC_GROUP, zeroDropGauges));
    }
    assertTrue(resubmitLatch.await(30, TimeUnit.SECONDS));
    assertTrue(autoScaleLatch.await(30 + 5, /* leeway */
    TimeUnit.SECONDS));
}
Also used : WorkerHost(io.mantisrx.server.core.WorkerHost) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) MantisMasterClientApi(io.mantisrx.server.master.client.MantisMasterClientApi) WorkerAssignments(io.mantisrx.server.core.WorkerAssignments) Func1(rx.functions.Func1) JobSchedulingInfo(io.mantisrx.server.core.JobSchedulingInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Observable(rx.Observable) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GaugeMeasurement(io.mantisrx.common.metrics.measurement.GaugeMeasurement) Test(org.junit.Test)

Aggregations

MantisMasterClientApi (io.mantisrx.server.master.client.MantisMasterClientApi)12 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 Matchers.anyString (org.mockito.Matchers.anyString)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 JobSchedulingInfo (io.mantisrx.server.core.JobSchedulingInfo)6 WorkerAssignments (io.mantisrx.server.core.WorkerAssignments)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Context (io.mantisrx.runtime.Context)5 MachineDefinition (io.mantisrx.runtime.MachineDefinition)5 SchedulingInfo (io.mantisrx.runtime.descriptor.SchedulingInfo)5 StageScalingPolicy (io.mantisrx.runtime.descriptor.StageScalingPolicy)5 StageSchedulingInfo (io.mantisrx.runtime.descriptor.StageSchedulingInfo)5 WorkerHost (io.mantisrx.server.core.WorkerHost)5 GaugeMeasurement (io.mantisrx.common.metrics.measurement.GaugeMeasurement)4 Observable (rx.Observable)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Map (java.util.Map)2 Func1 (rx.functions.Func1)2 MantisServerSentEvent (io.mantisrx.common.MantisServerSentEvent)1