Search in sources :

Example 1 with HystrixCommand

use of com.netflix.hystrix.HystrixCommand in project grakn by graknlabs.

the class BatchExecutorClientIT method whenSending100Queries_TheyAreSentInBatch.

@Ignore("This test interferes with other tests using the BEC (they probably use the same HystrixRequestLog)")
@Test
public void whenSending100Queries_TheyAreSentInBatch() {
    List<Observable<QueryResponse>> all = new ArrayList<>();
    // Increasing the max delay so eveyrthing goes in a single batch
    try (BatchExecutorClient loader = loader(MAX_DELAY * 100)) {
        int n = 100;
        generate(this::query).limit(n).forEach(q -> all.add(loader.add(q, keyspace, true)));
        int completed = allObservable(all).toBlocking().first().size();
        assertEquals(n, completed);
        assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
        HystrixCommand<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().toArray(new HystrixCommand<?>[1])[0];
        // assert the command is the one we're expecting
        assertEquals("CommandQueries", command.getCommandKey().name());
        // confirm that it was a COLLAPSED command execution
        assertTrue(command.getExecutionEvents().contains(HystrixEventType.COLLAPSED));
        // and that it was successful
        assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS));
    }
}
Also used : HystrixCommand(com.netflix.hystrix.HystrixCommand) ArrayList(java.util.ArrayList) BatchExecutorClient(ai.grakn.client.BatchExecutorClient) ConcurrencyUtil.allObservable(ai.grakn.util.ConcurrencyUtil.allObservable) Observable(rx.Observable) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with HystrixCommand

use of com.netflix.hystrix.HystrixCommand in project spring-cloud-sleuth by spring-cloud.

the class TraceCommandTests method should_pass_tracing_information_when_using_Hystrix_commands.

@Test
public void should_pass_tracing_information_when_using_Hystrix_commands() {
    Tracer tracer = this.tracer;
    TraceKeys traceKeys = new TraceKeys();
    HystrixCommand.Setter setter = withGroupKey(asKey("group")).andCommandKey(HystrixCommandKey.Factory.asKey("command"));
    // tag::hystrix_command[]
    HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) {

        @Override
        protected String run() throws Exception {
            return someLogic();
        }
    };
    // end::hystrix_command[]
    // tag::trace_hystrix_command[]
    TraceCommand<String> traceCommand = new TraceCommand<String>(tracer, traceKeys, setter) {

        @Override
        public String doRun() throws Exception {
            return someLogic();
        }
    };
    // end::trace_hystrix_command[]
    String resultFromHystrixCommand = hystrixCommand.execute();
    String resultFromTraceCommand = traceCommand.execute();
    then(resultFromHystrixCommand).isEqualTo(resultFromTraceCommand);
}
Also used : TraceKeys(org.springframework.cloud.sleuth.TraceKeys) HystrixCommand(com.netflix.hystrix.HystrixCommand) Tracer(brave.Tracer) Test(org.junit.Test)

Example 3 with HystrixCommand

use of com.netflix.hystrix.HystrixCommand in project data-prep by Talend.

the class CommandHelper method toPublisher.

/**
 * Return a Publisher of type T out of the the hystrix command.
 *
 * @param clazz the wanted stream type.
 * @param mapper the object mapper used to parse objects.
 * @param command the hystrix command to deal with.
 * @param <T> the type of objects to stream.
 * @return a Publisher<T></T> out of the hystrix command response body.
 */
public static <T> Publisher<T> toPublisher(final Class<T> clazz, final ObjectMapper mapper, final HystrixCommand<InputStream> command) {
    AtomicInteger count = new AtomicInteger(0);
    return Flux.create(sink -> {
        final Observable<InputStream> observable = command.toObservable();
        observable.map(i -> {
            try {
                return mapper.readerFor(clazz).<T>readValues(i);
            } catch (IOException e) {
                throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
            }
        }).doOnCompleted(// 
        () -> LOGGER.debug("Completed command '{}' (emits '{}') with '{}' records.", command.getClass().getName(), clazz.getName(), count.get())).toBlocking().forEach(s -> {
            while (s.hasNext()) {
                sink.next(s.next());
                count.incrementAndGet();
            }
            sink.complete();
        });
    }, FluxSink.OverflowStrategy.BUFFER);
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Logger(org.slf4j.Logger) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) FluxSink(reactor.core.publisher.FluxSink) LoggerFactory(org.slf4j.LoggerFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MultiValueMap(org.springframework.util.MultiValueMap) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) IOException(java.io.IOException) Header(org.apache.http.Header) Observable(rx.Observable) IOUtils(org.apache.commons.io.IOUtils) HttpStatus(org.springframework.http.HttpStatus) Flux(reactor.core.publisher.Flux) HystrixCommand(com.netflix.hystrix.HystrixCommand) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CommonErrorCodes(org.talend.dataprep.exception.error.CommonErrorCodes) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) InputStream(java.io.InputStream) TDPException(org.talend.dataprep.exception.TDPException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 4 with HystrixCommand

use of com.netflix.hystrix.HystrixCommand in project incubator-skywalking by apache.

the class HystrixCommandConstructorInterceptor method onConstruct.

@Override
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
    String commandIdentify = "";
    if (HystrixCommand.class.isAssignableFrom(objInst.getClass())) {
        HystrixCommand hystrixCommand = (HystrixCommand) objInst;
        commandIdentify = hystrixCommand.getCommandKey().name();
    } else if (HystrixCollapser.class.isAssignableFrom(objInst.getClass())) {
        HystrixCollapser hystrixCollapser = (HystrixCollapser) objInst;
        commandIdentify = hystrixCollapser.getCollapserKey().name();
    } else if (HystrixObservableCollapser.class.isAssignableFrom(objInst.getClass())) {
        HystrixObservableCollapser hystrixObservableCollapser = (HystrixObservableCollapser) objInst;
        commandIdentify = hystrixObservableCollapser.getCollapserKey().name();
    } else if (HystrixObservableCommand.class.isAssignableFrom(objInst.getClass())) {
        HystrixObservableCommand hystrixObservableCommand = (HystrixObservableCommand) objInst;
        commandIdentify = hystrixObservableCommand.getCommandKey().name();
    }
    objInst.setSkyWalkingDynamicField(new EnhanceRequireObjectCache(OPERATION_NAME_PREFIX + commandIdentify));
}
Also used : HystrixCommand(com.netflix.hystrix.HystrixCommand) HystrixCollapser(com.netflix.hystrix.HystrixCollapser) HystrixObservableCommand(com.netflix.hystrix.HystrixObservableCommand) HystrixObservableCollapser(com.netflix.hystrix.HystrixObservableCollapser)

Example 5 with HystrixCommand

use of com.netflix.hystrix.HystrixCommand in project ovirt-engine by oVirt.

the class HystrixBackendActionExecutor method execute.

@Override
public ActionReturnValue execute(final CommandBase<?> command) {
    final HystrixCommand.Setter setter = HystrixSettings.setter(command.getActionType().name());
    final HystrixCommand<ActionReturnValue> hystrixCommand = new HystrixCommand(setter) {

        @Override
        protected ActionReturnValue run() throws Exception {
            final ActionReturnValue returnValue = command.executeAction();
            if (returnValue.getSucceeded()) {
                return returnValue;
            }
            // throw this so that hystrix can see that this command failed
            throw new ActionFailedException(returnValue);
        }
    };
    try {
        return hystrixCommand.execute();
    } catch (HystrixRuntimeException e) {
        // only thrown for hystrix, so catch it and proceed normally
        if (e.getCause() instanceof ActionFailedException) {
            return ((ActionFailedException) e.getCause()).getReturnValue();
        }
        throw e;
    }
}
Also used : HystrixCommand(com.netflix.hystrix.HystrixCommand) ActionReturnValue(org.ovirt.engine.core.common.action.ActionReturnValue) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException)

Aggregations

HystrixCommand (com.netflix.hystrix.HystrixCommand)6 Test (org.junit.Test)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Observable (rx.Observable)2 BatchExecutorClient (ai.grakn.client.BatchExecutorClient)1 ConcurrencyUtil.allObservable (ai.grakn.util.ConcurrencyUtil.allObservable)1 Tracer (brave.Tracer)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HystrixCollapser (com.netflix.hystrix.HystrixCollapser)1 HystrixObservableCollapser (com.netflix.hystrix.HystrixObservableCollapser)1 HystrixObservableCommand (com.netflix.hystrix.HystrixObservableCommand)1 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Stream (java.util.stream.Stream)1 IOUtils (org.apache.commons.io.IOUtils)1 Header (org.apache.http.Header)1 Ignore (org.junit.Ignore)1