Search in sources :

Example 31 with TSerializer

use of org.apache.thrift.TSerializer in project vcell by virtualcell.

the class CopasiOptimizationSolver method solveRemoteApi.

public static OptimizationResultSet solveRemoteApi(ParameterEstimationTaskSimulatorIDA parestSimulator, ParameterEstimationTask parameterEstimationTask, CopasiOptSolverCallbacks optSolverCallbacks, MathMappingCallback mathMappingCallback, ClientTaskStatusSupport clientTaskStatusSupport) throws IOException, ExpressionException, OptimizationException {
    try {
        if (clientTaskStatusSupport != null) {
            clientTaskStatusSupport.setMessage("Generating opt problem...");
        }
        OptProblem optProblem = CopasiServicePython.makeOptProblem(parameterEstimationTask);
        boolean bIgnoreCertProblems = true;
        boolean bIgnoreHostMismatch = true;
        // e.g. vcell.serverhost=vcellapi.cam.uchc.edu:443
        String serverHost = PropertyLoader.getRequiredProperty(PropertyLoader.vcellServerHost);
        String[] parts = serverHost.split(":");
        String host = parts[0];
        int port = Integer.parseInt(parts[1]);
        VCellApiClient apiClient = new VCellApiClient(host, port, bIgnoreCertProblems, bIgnoreHostMismatch);
        TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
        String optProblemJson = serializer.toString(optProblem);
        if (clientTaskStatusSupport != null) {
            clientTaskStatusSupport.setMessage("Submitting opt problem...");
        }
        // Submit but allow user to get out from restlet blocking call
        final String[] optIdHolder = new String[] { null };
        final Exception[] exceptHolder = new Exception[] { null };
        Thread submitThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    optIdHolder[0] = apiClient.submitOptimization(optProblemJson);
                    if (optSolverCallbacks.getStopRequested()) {
                        apiClient.getOptRunJson(optIdHolder[0], optSolverCallbacks.getStopRequested());
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    exceptHolder[0] = e;
                }
            }
        });
        submitThread.setDaemon(true);
        submitThread.start();
        while (optIdHolder[0] == null && exceptHolder[0] == null && !optSolverCallbacks.getStopRequested()) {
            Thread.sleep(200);
        }
        if (exceptHolder[0] != null) {
            throw exceptHolder[0];
        }
        if (optSolverCallbacks.getStopRequested()) {
            throw UserCancelException.CANCEL_GENERIC;
        }
        // 200 second timeout
        final long TIMEOUT_MS = 1000 * 200;
        long startTime = System.currentTimeMillis();
        OptRun optRun = null;
        if (clientTaskStatusSupport != null) {
            clientTaskStatusSupport.setMessage("Waiting for progress...");
        }
        while ((System.currentTimeMillis() - startTime) < TIMEOUT_MS) {
            String optRunJson = apiClient.getOptRunJson(optIdHolder[0], optSolverCallbacks.getStopRequested());
            if (optSolverCallbacks.getStopRequested()) {
                throw UserCancelException.CANCEL_GENERIC;
            }
            if (optRunJson.startsWith(OptRunStatus.Queued.name() + ":")) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        optSolverCallbacks.setEvaluation(0, 0, 0, optSolverCallbacks.getEndValue(), 0);
                    }
                });
                if (clientTaskStatusSupport != null) {
                    clientTaskStatusSupport.setMessage("Queued...");
                }
            } else if (optRunJson.startsWith("Failed:") || optRunJson.startsWith("exception:") || optRunJson.startsWith("Exception:")) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        optSolverCallbacks.setEvaluation(0, 0, 0, optSolverCallbacks.getEndValue(), 0);
                    }
                });
                if (clientTaskStatusSupport != null) {
                    clientTaskStatusSupport.setMessage(optRunJson);
                }
            } else if (optRunJson.startsWith(OptRunStatus.Running.name() + ":")) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            StringTokenizer st = new StringTokenizer(optRunJson, " :\t\r\n");
                            if (st.countTokens() != 4) {
                                System.out.println(optRunJson);
                                return;
                            }
                            // OptRunStatus mesg
                            st.nextToken();
                            int runNum = Integer.parseInt(st.nextToken());
                            double objFunctionValue = Double.parseDouble(st.nextToken());
                            int numObjFuncEvals = Integer.parseInt(st.nextToken());
                            SwingUtilities.invokeLater(new Runnable() {

                                @Override
                                public void run() {
                                    optSolverCallbacks.setEvaluation(numObjFuncEvals, objFunctionValue, 1.0, null, runNum);
                                }
                            });
                        } catch (Exception e) {
                            System.out.println(optRunJson);
                            e.printStackTrace();
                        }
                    }
                });
                if (clientTaskStatusSupport != null) {
                    clientTaskStatusSupport.setMessage("Running...");
                }
            } else {
                // File f = new File("/home/vcell/fake_share_apps_vcell3/users/ParamOptemize_"+optimizationId+"/ParamOptemize_"+optimizationId+"_optRun.bin");
                // byte[] filesbytes = FileUtils.readFileToByteArray(f);
                // optRunJson.getBytes();
                byte[] jsonbytes = Base64.getDecoder().decode(optRunJson);
                // System.out.println(filesbytes.length+" "+jsonbytes.length);
                // for (int i = 0; i < filesbytes.length; i++) {
                // if(filesbytes[i] != jsonbytes[i]) {
                // System.out.println("differ at "+i);
                // break;
                // }
                // }
                TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
                // TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory());
                optRun = new OptRun();
                deserializer.deserialize(optRun, jsonbytes);
                OptRunStatus status = optRun.status;
                String statusMessage = optRun.getStatusMessage();
                if (statusMessage != null && (statusMessage.toLowerCase().startsWith(OptRunStatus.Complete.name().toLowerCase()))) {
                    final OptRun or2 = optRun;
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            // Double endValue = null;
                            // for (org.vcell.optimization.thrift.CopasiOptimizationParameter cop : or2.getOptProblem().getOptimizationMethod().getOptimizationParameterList()) {
                            // if (cop.getParamType().name().equals(CopasiOptimizationParameterType.Number_of_Generations.name())
                            // || cop.getParamType().name().equals(CopasiOptimizationParameterType.IterationLimit.name())){
                            // endValue = cop.getValue();
                            // break;
                            // }
                            // }
                            optSolverCallbacks.setEvaluation((int) or2.getOptResultSet().getNumFunctionEvaluations(), or2.getOptResultSet().objectiveFunction, 1.0, null, or2.getOptProblem().numberOfOptimizationRuns);
                        }
                    });
                // }
                }
                if (status == OptRunStatus.Complete) {
                    System.out.println("job " + optIdHolder[0] + ": status " + status + " " + optRun.getOptResultSet().toString());
                    if (clientTaskStatusSupport != null) {
                        clientTaskStatusSupport.setProgress(100);
                    }
                    break;
                }
                if (status == OptRunStatus.Failed) {
                    throw new RuntimeException("optimization failed, message=" + optRun.statusMessage);
                }
                System.out.println("job " + optIdHolder[0] + ": status " + status);
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }
        if ((System.currentTimeMillis() - startTime) >= TIMEOUT_MS) {
            throw new RuntimeException("optimization timed out.");
        }
        System.out.println("done with optimization");
        OptResultSet optResultSet = optRun.getOptResultSet();
        if (optResultSet == null) {
            throw new RuntimeException("optResultSet is null, status is " + optRun.getStatusMessage());
        }
        if (optResultSet != null && optResultSet.getOptParameterValues() == null) {
            throw new RuntimeException("getOptParameterValues is null, status is " + optRun.getStatusMessage());
        }
        if (clientTaskStatusSupport != null) {
            clientTaskStatusSupport.setMessage("Done, getting results...");
        }
        int numFittedParameters = optResultSet.getOptParameterValues().size();
        String[] paramNames = new String[numFittedParameters];
        double[] paramValues = new double[numFittedParameters];
        for (int pIndex = 0; pIndex < numFittedParameters; pIndex++) {
            OptParameterValue optParamValue = optResultSet.getOptParameterValues().get(pIndex);
            paramNames[pIndex] = optParamValue.parameterName;
            paramValues[pIndex] = optParamValue.bestValue;
        }
        OptimizationStatus status = new OptimizationStatus(OptimizationStatus.NORMAL_TERMINATION, optRun.statusMessage);
        OptRunResultSet optRunResultSet = new OptRunResultSet(paramValues, optResultSet.objectiveFunction, optResultSet.numFunctionEvaluations, status);
        OptSolverResultSet copasiOptSolverResultSet = new OptSolverResultSet(paramNames, optRunResultSet);
        RowColumnResultSet copasiRcResultSet = parestSimulator.getRowColumnRestultSetByBestEstimations(parameterEstimationTask, paramNames, paramValues);
        OptimizationResultSet copasiOptimizationResultSet = new OptimizationResultSet(copasiOptSolverResultSet, copasiRcResultSet);
        System.out.println("-----------SOLUTION FROM VCellAPI---------------\n" + optResultSet.toString());
        return copasiOptimizationResultSet;
    } catch (UserCancelException e) {
        throw e;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new OptimizationException(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
    }
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) TDeserializer(org.apache.thrift.TDeserializer) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) UserCancelException(org.vcell.util.UserCancelException) OptParameterValue(org.vcell.optimization.thrift.OptParameterValue) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) OptRunStatus(org.vcell.optimization.thrift.OptRunStatus) OptRun(org.vcell.optimization.thrift.OptRun) OptResultSet(org.vcell.optimization.thrift.OptResultSet) OptimizationStatus(cbit.vcell.opt.OptimizationStatus) RowColumnResultSet(cbit.vcell.math.RowColumnResultSet) OptProblem(org.vcell.optimization.thrift.OptProblem) VCellApiClient(org.vcell.api.client.VCellApiClient) OptimizationException(cbit.vcell.opt.OptimizationException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) UserCancelException(org.vcell.util.UserCancelException) TSerializer(org.apache.thrift.TSerializer) StringTokenizer(java.util.StringTokenizer) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) OptRunResultSet(cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)

Example 32 with TSerializer

use of org.apache.thrift.TSerializer in project jstorm by alibaba.

the class Utils method thriftSerialize.

public static byte[] thriftSerialize(TBase t) {
    try {
        TSerializer ser = threadSer.get();
        if (ser == null) {
            ser = new TSerializer();
            threadSer.set(ser);
        }
        return ser.serialize(t);
    } catch (TException e) {
        LOG.error("Failed to serialize to thrift: ", e);
        throw new RuntimeException(e);
    }
}
Also used : TException(org.apache.thrift.TException) TSerializer(org.apache.thrift.TSerializer)

Example 33 with TSerializer

use of org.apache.thrift.TSerializer in project jstorm by alibaba.

the class JStormUtils method getSer.

private static TSerializer getSer() {
    TSerializer ser = threadSer.get();
    if (ser == null) {
        ser = new TSerializer();
        threadSer.set(ser);
    }
    return ser;
}
Also used : TSerializer(org.apache.thrift.TSerializer)

Example 34 with TSerializer

use of org.apache.thrift.TSerializer in project aerosolve by airbnb.

the class Debug method saveExample.

// Save example to path
// If you hit permission error, touch and chmod the file
public static void saveExample(Example example, String path) {
    TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
    try {
        byte[] buf = serializer.serialize(example);
        FileOutputStream fos = new FileOutputStream(path);
        fos.write(buf);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TSerializer(org.apache.thrift.TSerializer) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) FileOutputStream(java.io.FileOutputStream)

Example 35 with TSerializer

use of org.apache.thrift.TSerializer in project eiger by wlloyd.

the class RangeSliceCommandSerializer method serialize.

public void serialize(RangeSliceCommand sliceCommand, DataOutput dos, int version) throws IOException {
    dos.writeUTF(sliceCommand.keyspace);
    dos.writeUTF(sliceCommand.column_family);
    ByteBuffer sc = sliceCommand.super_column;
    dos.writeInt(sc == null ? 0 : sc.remaining());
    if (sc != null)
        ByteBufferUtil.write(sc, dos);
    TSerializer ser = new TSerializer(new TBinaryProtocol.Factory());
    FBUtilities.serialize(ser, sliceCommand.predicate, dos);
    if (version >= MessagingService.VERSION_11) {
        if (sliceCommand.row_filter == null) {
            dos.writeInt(0);
        } else {
            dos.writeInt(sliceCommand.row_filter.size());
            for (IndexExpression expr : sliceCommand.row_filter) FBUtilities.serialize(ser, expr, dos);
        }
    }
    AbstractBounds.serializer().serialize(sliceCommand.range, dos, version);
    dos.writeInt(sliceCommand.maxResults);
    if (version >= MessagingService.VERSION_11) {
        dos.writeBoolean(sliceCommand.maxIsColumns);
    }
}
Also used : TSerializer(org.apache.thrift.TSerializer) TBinaryProtocol(org.apache.cassandra.thrift.TBinaryProtocol) IndexExpression(org.apache.cassandra.thrift.IndexExpression) ByteBuffer(java.nio.ByteBuffer)

Aggregations

TSerializer (org.apache.thrift.TSerializer)35 TException (org.apache.thrift.TException)19 TJSONProtocol (org.apache.thrift.protocol.TJSONProtocol)14 IOException (java.io.IOException)12 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)8 Test (org.junit.Test)6 TDeserializer (org.apache.thrift.TDeserializer)5 ArrayList (java.util.ArrayList)4 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)4 OptRun (org.vcell.optimization.thrift.OptRun)4 Table (org.apache.hadoop.hive.metastore.api.Table)3 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)3 VCellApiClient (org.vcell.api.client.VCellApiClient)3 OptProblem (org.vcell.optimization.thrift.OptProblem)3 OptRunStatus (org.vcell.optimization.thrift.OptRunStatus)3 MathException (cbit.vcell.math.MathException)2 RowColumnResultSet (cbit.vcell.math.RowColumnResultSet)2 OptSolverResultSet (cbit.vcell.opt.OptSolverResultSet)2 OptRunResultSet (cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)2 OptimizationException (cbit.vcell.opt.OptimizationException)2