Example usage for org.apache.thrift.transport TTransport flush

List of usage examples for org.apache.thrift.transport TTransport flush

Introduction

In this page you can find the example usage for org.apache.thrift.transport TTransport flush.

Prototype

public void flush() throws TTransportException 

Source Link

Document

Flush any pending data out of a transport buffer.

Usage

From source file:com.facebook.buck.cli.BuildCommand.java

License:Apache License

private int executeDistributedBuild(final CommandRunnerParams params,
        final WeightedListeningExecutorService executorService)
        throws IOException, InterruptedException, ActionGraphCreationException {
    ProjectFilesystem filesystem = params.getCell().getFilesystem();

    if (distributedBuildStateFile != null) {
        Path stateDumpPath = Paths.get(distributedBuildStateFile);
        TTransport transport;
        boolean loading = Files.exists(stateDumpPath);
        if (loading) {
            transport = new TIOStreamTransport(filesystem.newFileInputStream(stateDumpPath));
        } else {//  w  w w  . j a  v a 2s .  com
            transport = new TIOStreamTransport(filesystem.newFileOutputStream(stateDumpPath));
        }
        transport = new TZlibTransport(transport);
        TProtocol protocol = new TTupleProtocol(transport);

        try {
            DistributedBuildTypeCoercerFactory typeCoercerFactory = new DistributedBuildTypeCoercerFactory(
                    params.getObjectMapper());
            ParserTargetNodeFactory<TargetNode<?>> parserTargetNodeFactory = DefaultParserTargetNodeFactory
                    .createForDistributedBuild(new ConstructorArgMarshaller(typeCoercerFactory),
                            new TargetNodeFactory(typeCoercerFactory));
            DistributedBuildTargetGraphCodec targetGraphCodec = new DistributedBuildTargetGraphCodec(
                    params.getObjectMapper(), parserTargetNodeFactory,
                    new Function<TargetNode<?>, Map<String, Object>>() {
                        @Nullable
                        @Override
                        public Map<String, Object> apply(TargetNode<?> input) {
                            try {
                                return params.getParser().getRawTargetNode(params.getBuckEventBus(),
                                        params.getCell().getCell(input.getBuildTarget()),
                                        /* enableProfiling */ false, executorService, input);
                            } catch (BuildFileParseException | InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    });

            if (loading) {
                DistributedBuildState state = DistributedBuildState.load(protocol, params.getCell());
                final TargetGraph targetGraph = state.createTargetGraph(targetGraphCodec);

                ActionGraphAndResolver actionGraphAndResolver = Preconditions
                        .checkNotNull(params.getActionGraphCache().getActionGraph(params.getBuckEventBus(),
                                /* checkActionGraphs */ false, targetGraph,
                                params.getBuckConfig().getKeySeed()));
                BuckConfig rootCellBuckConfig = state.getRootCell().getBuckConfig();
                DistributedCachingBuildEngineDelegate cachingBuildEngineDelegate = new DistributedCachingBuildEngineDelegate(
                        new SourcePathResolver(actionGraphAndResolver.getResolver()), state);

                // TODO(ruibm, marcinkosiba): This is incorrect, we probably need rule-level control
                // over what gets built.
                ImmutableList<TargetNodeSpec> targetNodeSpecs = parseArgumentsAsTargetNodeSpecs(
                        rootCellBuckConfig, getArguments());
                FluentIterable<BuildTarget> targetsToBuild = FluentIterable.from(targetNodeSpecs)
                        .transformAndConcat(new Function<TargetNodeSpec, Iterable<BuildTarget>>() {
                            @Override
                            public Iterable<BuildTarget> apply(TargetNodeSpec input) {
                                return input.filter(targetGraph.getNodes()).keySet();
                            }
                        });
                return executeBuild(params, actionGraphAndResolver, executorService, params.getArtifactCache(),
                        cachingBuildEngineDelegate, rootCellBuckConfig, targetsToBuild);
            } else {
                TargetGraphAndBuildTargets targetGraphAndBuildTargets = createTargetGraph(params,
                        executorService);
                ActionGraphAndResolver actionGraphAndResolver = createActionGraphAndResolver(params,
                        targetGraphAndBuildTargets);
                DistributedBuildCellIndexer cellIndexer = new DistributedBuildCellIndexer(params.getCell());
                DistributedBuildFileHashes distributedBuildFileHashes = new DistributedBuildFileHashes(
                        actionGraphAndResolver.getActionGraph(),
                        new SourcePathResolver(actionGraphAndResolver.getResolver()), params.getFileHashCache(),
                        cellIndexer, executorService, params.getBuckConfig().getKeySeed());
                BuildJobState jobState = DistributedBuildState.dump(cellIndexer, distributedBuildFileHashes,
                        targetGraphCodec, targetGraphAndBuildTargets.getTargetGraph());
                jobState.write(protocol);
                transport.flush();
                return 0;
            }
        } catch (TException e) {
            throw new RuntimeException(e);
        } finally {
            transport.close();
        }
    }

    DistBuildConfig config = new DistBuildConfig(params.getBuckConfig());
    ClientSideSlb slb = config.getFrontendConfig().createHttpClientSideSlb(params.getClock(),
            params.getBuckEventBus(), new CommandThreadFactory("BuildCommand.HttpLoadBalancer"));
    OkHttpClient client = config.createOkHttpClient();

    try (HttpService httpService = new LoadBalancedService(slb, client, params.getBuckEventBus());
            ThriftService<FrontendRequest, FrontendResponse> service = new ThriftOverHttpService<>(
                    ThriftOverHttpServiceConfig.of(httpService))) {
        DistributedBuild build = new DistributedBuild(new DistBuildService(service, params.getBuckEventBus()));
        return build.executeAndPrintFailuresToEventBus();
    }
}

From source file:com.jredrain.job.RedRainCaller.java

License:Apache License

public Response call(Request request, Agent agent) throws Exception {

    //?...//from w  ww.  java2 s. c o  m
    if (agent.getProxy() == RedRain.ConnType.PROXY.getType()) {
        Map<String, String> proxyParams = new HashMap<String, String>(0);
        proxyParams.put("proxyHost", request.getHostName());
        proxyParams.put("proxyPort", request.getPort() + "");
        proxyParams.put("proxyAction", request.getAction().name());
        proxyParams.put("proxyPassword", request.getPassword());
        if (CommonUtils.notEmpty(request.getParams())) {
            proxyParams.put("proxyParams", JSON.toJSONString(request.getParams()));
        }

        Agent proxyAgent = agentService.getAgent(agent.getProxyAgent());
        request.setHostName(proxyAgent.getIp());
        request.setPort(proxyAgent.getPort());
        request.setAction(Action.PROXY);
        request.setPassword(proxyAgent.getPassword());
        request.setParams(proxyParams);
    }

    TTransport transport;
    /**
     * ping5,
     */
    if (request.getAction().equals(Action.PING)) {
        transport = new TSocket(request.getHostName(), request.getPort(), 1000 * 5);
    } else {
        transport = new TSocket(request.getHostName(), request.getPort());
    }
    TProtocol protocol = new TBinaryProtocol(transport);
    RedRain.Client client = new RedRain.Client(protocol);
    transport.open();

    Response response = null;
    for (Method method : client.getClass().getMethods()) {
        if (method.getName().equalsIgnoreCase(request.getAction().name())) {
            response = (Response) method.invoke(client, request);
            break;
        }
    }

    transport.flush();
    transport.close();
    return response;
}

From source file:com.jredrain.startup.AgentProcessor.java

License:Apache License

@Override
public Response proxy(Request request) throws TException {
    String proxyHost = request.getParams().get("proxyHost");
    String proxyPort = request.getParams().get("proxyPort");
    String proxyAction = request.getParams().get("proxyAction");
    String proxyPassword = request.getParams().get("proxyPassword");

    //?..../*  ww  w  .  ja  va2  s  .  c om*/
    String proxyParams = request.getParams().get("proxyParams");
    Map<String, String> params = new HashMap<String, String>(0);
    if (CommonUtils.notEmpty(proxyParams)) {
        params = (Map<String, String>) JSON.parse(proxyParams);
    }

    Request proxyReq = Request
            .request(proxyHost, toInt(proxyPort), Action.findByName(proxyAction), proxyPassword)
            .setParams(params);

    logger.info("[redrain]proxy params:{}", proxyReq.toString());

    TTransport transport = null;
    /**
     * ping5,
     */
    if (proxyReq.getAction().equals(Action.PING)) {
        transport = new TSocket(proxyReq.getHostName(), proxyReq.getPort(), 1000 * 5);
    } else {
        transport = new TSocket(proxyReq.getHostName(), proxyReq.getPort());
    }
    TProtocol protocol = new TBinaryProtocol(transport);
    RedRain.Client client = new RedRain.Client(protocol);
    transport.open();

    Response response = null;
    for (Method method : client.getClass().getMethods()) {
        if (method.getName().equalsIgnoreCase(proxyReq.getAction().name())) {
            try {
                response = (Response) method.invoke(client, proxyReq);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            break;
        }
    }
    transport.flush();
    transport.close();
    return response;
}

From source file:com.jwsphere.accumulo.TransportCompression.java

License:Apache License

private long computeSize(List<TKeyValue> data) {
    ScanResult result = new ScanResult(data, false);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TTransport transport = new TIOStreamTransport(baos);
    TCompactProtocol proto = new TCompactProtocol(transport);
    try {//from   w  w w .  jav a  2 s.  c  om
        result.write(proto);
        transport.flush();
    } catch (TException e) {
        throw new RuntimeException(e);
    }
    return baos.toByteArray().length;
}

From source file:lizard.comms.TestLzClientServer.java

License:Apache License

@Test
public void wire_01() throws Exception {
    Handler handler = new Handler() {
        @Override// w ww.ja v a 2 s. c  o m
        public void handle(TTransport transport) {
            try {
                TProtocol proto = ThriftLib.protocol(transport);
                TLZ_Ping p = new TLZ_Ping();
                p.read(proto);
                p.write(proto);
                transport.flush();
            } catch (TException ex) {
                handlerException.set(ex);
            }
        }
    };
    ThriftServerAcceptFork server = new ThriftServerAcceptFork(test_port, handler);

    server.start();

    ThriftClient c = new ThriftClient("localhost", test_port);
    c.start();
    TProtocol proto = c.protocol();
    TLZ_Ping p = new TLZ_Ping(1324);
    p.write(proto);
    proto.getTransport().flush();
    TLZ_Ping p2 = new TLZ_Ping();
    p2.read(proto);
    assertEquals(p.getMarker(), p2.getMarker());
    assertNull(handlerException.get());
    c.stop();
    server.stop();

}

From source file:net.morimekta.providence.thrift.TProtocolSerializer.java

License:Apache License

@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(OutputStream output,
        Message message) throws IOException {
    CountingOutputStream wrapper = new CountingOutputStream(output);
    TTransport transport = new TIOStreamTransport(wrapper);
    try {/*from   ww w.  ja va  2s .  c o  m*/
        TProtocol protocol = protocolFactory.getProtocol(transport);
        writeMessage(message, protocol);
        transport.flush();
        wrapper.flush();
        return wrapper.getByteCount();
    } catch (TException e) {
        throw new SerializerException(e, e.getMessage());
    }
}

From source file:net.morimekta.providence.thrift.TProtocolSerializer.java

License:Apache License

@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(OutputStream output,
        PServiceCall<Message, Field> call) throws IOException {
    CountingOutputStream wrapper = new CountingOutputStream(output);
    TTransport transport = new TIOStreamTransport(wrapper);
    try {/*w w  w .j av a2  s.  co  m*/
        TProtocol protocol = protocolFactory.getProtocol(transport);
        TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().getValue(), call.getSequence());

        protocol.writeMessageBegin(tm);
        writeMessage(call.getMessage(), protocol);
        protocol.writeMessageEnd();

        transport.flush();
        wrapper.flush();
        return wrapper.getByteCount();
    } catch (TException e) {
        throw new SerializerException(e, e.getMessage());
    }
}

From source file:net.morimekta.providence.thrift.TTupleProtocolSerializer.java

License:Apache License

@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(OutputStream output,
        Message message) throws IOException {
    CountingOutputStream wrapper = new CountingOutputStream(output);
    TTransport transport = new TIOStreamTransport(wrapper);
    try {/*from   w  ww .j a v  a 2 s .  c  o m*/
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        writeMessage(message, protocol);
        transport.flush();
        wrapper.flush();
        return wrapper.getByteCount();
    } catch (TException e) {
        throw new SerializerException(e, e.getMessage());
    }
}

From source file:net.morimekta.providence.thrift.TTupleProtocolSerializer.java

License:Apache License

@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(OutputStream output,
        PServiceCall<Message, Field> call) throws IOException {
    CountingOutputStream wrapper = new CountingOutputStream(output);
    TTransport transport = new TIOStreamTransport(wrapper);
    try {//from   w  w  w.j  a  v a 2  s .  c  o  m
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().getValue(), call.getSequence());

        protocol.writeMessageBegin(tm);
        writeMessage(call.getMessage(), protocol);
        protocol.writeMessageEnd();

        transport.flush();
        wrapper.flush();
        return wrapper.getByteCount();
    } catch (TException e) {
        throw new SerializerException(e, e.getMessage());
    }
}

From source file:org.apache.accumulo.monitor.ZooKeeperStatus.java

License:Apache License

@Override
public void run() {

    while (!stop) {

        TreeSet<ZooKeeperState> update = new TreeSet<>();

        String zookeepers[] = SiteConfiguration.getInstance().get(Property.INSTANCE_ZK_HOST).split(",");
        for (String keeper : zookeepers) {
            int clients = 0;
            String mode = "unknown";

            String[] parts = keeper.split(":");
            TTransport transport = null;
            try {
                HostAndPort addr;//from  w w  w  . ja  v  a 2  s . co  m
                if (parts.length > 1)
                    addr = HostAndPort.fromParts(parts[0], Integer.parseInt(parts[1]));
                else
                    addr = HostAndPort.fromParts(parts[0], 2181);

                transport = TTimeoutTransport.create(addr, 10 * 1000l);
                transport.write("stat\n".getBytes(UTF_8), 0, 5);
                StringBuilder response = new StringBuilder();
                try {
                    transport.flush();
                    byte[] buffer = new byte[1024 * 100];
                    int n = 0;
                    while ((n = transport.read(buffer, 0, buffer.length)) > 0) {
                        response.append(new String(buffer, 0, n, UTF_8));
                    }
                } catch (TTransportException ex) {
                    // happens at EOF
                }
                for (String line : response.toString().split("\n")) {
                    if (line.startsWith(" "))
                        clients++;
                    if (line.startsWith("Mode"))
                        mode = line.split(":")[1];
                }
                update.add(new ZooKeeperState(keeper, mode, clients));
            } catch (Exception ex) {
                log.info("Exception talking to zookeeper " + keeper, ex);
                update.add(new ZooKeeperState(keeper, "Down", -1));
            } finally {
                if (transport != null) {
                    try {
                        transport.close();
                    } catch (Exception ex) {
                        log.error("Exception", ex);
                    }
                }
            }
        }
        status = update;
        UtilWaitThread.sleep(5 * 1000);
    }
}