aboutsummaryrefslogtreecommitdiffstats
path: root/filedistribution/src/main/java/com/yahoo/vespa/filedistribution/RpcTester.java
blob: c9dc1d4ddd99f7a81a2c3e7edc83426a49c6252d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.filedistribution;

import com.yahoo.config.FileReference;
import com.yahoo.io.IOUtils;
import com.yahoo.jrt.DataValue;
import com.yahoo.jrt.Int32Value;
import com.yahoo.jrt.Int64Value;
import com.yahoo.jrt.Request;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.StringValue;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Target;
import com.yahoo.jrt.Transport;

import java.time.Duration;
import java.util.logging.Level;
import net.jpountz.xxhash.XXHash64;
import net.jpountz.xxhash.XXHashFactory;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Logger;

public class RpcTester {

    private static final Logger log = Logger.getLogger(RpcTester.class.getName());

    private final Target target;

    private RpcTester(Target target) {
        this.target = target;
    }

    private void call(String fileReference, String filename, byte[] blob) {
        new FileReceiver(target).receive(new FileReference(fileReference), filename, blob);
    }

    public static void main(String[] args) {
        //String fileReference = args[0];
        String fileReference = "59f93f445438c9db7ccbf1629f583c2aa004a68b";
        String filename = "com.yahoo.vespatest.ExtraHitSearcher-1.0.0-deploy.jar";
        File file = new File(String.format("/tmp/%s/%s", fileReference, filename));
        byte[] blob = null;

        try {
            blob = IOUtils.readFileBytes(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        log.log(Level.INFO, "Read blob from " + file.getAbsolutePath());


        Supervisor supervisor = new Supervisor(new Transport("rpctester"));

        Spec spec = new Spec("tcp/localhost:19090");
        log.log(Level.INFO, "Connecting to " + spec);
        Target target = supervisor.connect(spec);
        if (! target.isValid()) {
            log.log(Level.INFO, "Could not connect");
            System.exit(1);
        } else {
            log.log(Level.INFO, "Connected to " + spec);
        }

        new RpcTester(target).call(fileReference, filename, blob);
    }

    class FileReceiver {

        Target target;

        FileReceiver(Target target) {
            this.target = target;
        }

        void receive(FileReference reference, String filename, byte[] content) {

            log.log(Level.INFO, "Preparing receive call for " + reference.value() + " and file " + filename);

            XXHash64 hasher = XXHashFactory.fastestInstance().hash64();
            Request fileBlob = new Request("filedistribution.receiveFile");

            log.log(Level.INFO, "Calling " + fileBlob.methodName() + " with target " + target);

            fileBlob.parameters().add(new StringValue(reference.value()));
            fileBlob.parameters().add(new StringValue(filename));
            fileBlob.parameters().add(new DataValue(content));
            fileBlob.parameters().add(new Int64Value(hasher.hash(ByteBuffer.wrap(content), 0)));
            fileBlob.parameters().add(new Int32Value(0));
            fileBlob.parameters().add(new StringValue("OK"));
            log.log(Level.INFO, "Doing invokeSync");
            target.invokeSync(fileBlob, Duration.ofSeconds(5));
            log.log(Level.INFO, "Done with invokeSync");
        }
    }
}