summaryrefslogtreecommitdiffstats
path: root/filedistributionmanager/src/main/java/com/yahoo/vespa/filedistribution/FileDistributionManager.java
blob: 0a803269ff6980dd5968552774af2b604400207e (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.filedistribution;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.locks.Lock;

/**
 * @author tonytv
 */
public class FileDistributionManager {

    private final static boolean available;

    @SuppressWarnings({"UnusedDeclaration"}) // Needs to be here due to JNI
    private long nativeFileDistributionManager;

    private final File applicationDirectory;
    private final String appId;
    private final Lock lock;


    private native static void setup();

    private native void init(byte[] fileDbDirectory, byte[] zkServers);

    private native String addFileImpl(byte[] absolutePath);

    private native void setDeployedFilesImpl(byte[] host, byte[] appId, byte[][] fileReferences);

    private native void limitSendingOfDeployedFilesToImpl(byte[][] hostNames, byte[] appId);
    private native void limitFilesToImpl(byte[][] fileReferences);
    private native void removeDeploymentsThatHaveDifferentApplicationIdImpl(byte[][] asByteArrays, byte[] bytes);

    private native byte[] getProgressImpl(byte[] fileReference,
                                          byte[][] hostNames);

    private byte[][] getAsByteArrays(Collection<String> strings) {
        byte[][] byteArrays = new byte[strings.size()][];
        int i = 0;
        for (String string : strings) {
            byteArrays[i++] = string.getBytes();
        }
        return byteArrays;
    }

    @Override
    protected void finalize() throws Throwable {
        shutdown();
        super.finalize();
    }

    static {
        available = loadLibrary("filedistributionmanager");
        if (available)
            setup();
    }

    private static boolean loadLibrary(String name) {
        try {
            System.loadLibrary(name);
            return true;
        }
        catch (UnsatisfiedLinkError e) {
            return false;
        }
    }

    /** Returns whether this functionality is available in this runtime */
    public static boolean isAvailable() { return available; }

    private byte[] absolutePath(File file) {
        return file.getAbsolutePath().getBytes();
    }

    private void ensureDirExists(File dir) {
        if (!dir.exists()) {
            throw new PathDoesNotExistException(dir.getPath());
        }
    }

    public FileDistributionManager(File fileDbDirectory, File applicationDirectory, String zkServers, String appId, Lock lock) {
        ensureDirExists(applicationDirectory);
        ensureDirExists(fileDbDirectory);

        this.applicationDirectory = applicationDirectory;
        this.appId = appId;
        this.lock = lock;

        init(fileDbDirectory.getPath().getBytes(), zkServers.getBytes());
    }

    public String addFile(String relativePath) {
        File path = new File(applicationDirectory, relativePath);
        if (!path.exists())
            throw new PathDoesNotExistException(path.getPath());

        try (LockGuard guard = new LockGuard(lock)) {
            return addFileImpl(absolutePath(path));
        }
    }

    public void setDeployedFiles(String hostName, Collection<String> fileReferences) {
        try (LockGuard guard = new LockGuard(lock)) {
            setDeployedFilesImpl(hostName.getBytes(), appId.getBytes(), getAsByteArrays(fileReferences));
        }
    }

    public void reloadDeployFileDistributor() {
        try (LockGuard guard = new LockGuard(lock)) {
            /*
             * Try sending signal to vespa-filedistributor and
             * filedistributor processes. Note 15 char limit for process
             * name on Linux, see pkill manual page for details.
	     */
            Runtime.getRuntime().exec("pkill -SIGUSR1 ^(vespa-filedistr|filedistributor)$");
        } catch (IOException e) {
            throw new RuntimeException("Failed to reinitialize the filedistributor", e);
        }
    }

    public void limitSendingOfDeployedFilesTo(Collection<String> hostNames) {
        try (LockGuard guard = new LockGuard(lock)) {
            limitSendingOfDeployedFilesToImpl(getAsByteArrays(hostNames), appId.getBytes());
        }
    }

    public byte[] getProgress(String fileReference,
                              List<String> hostNamesSortedAscending) {
        return getProgressImpl(fileReference.getBytes(), getAsByteArrays(hostNamesSortedAscending));
    }


    public native void shutdown();

    public void removeDeploymentsThatHaveDifferentApplicationId(Collection<String> targetHostnames) {
        try (LockGuard guard = new LockGuard(lock)) {
            removeDeploymentsThatHaveDifferentApplicationIdImpl(getAsByteArrays(targetHostnames), appId.getBytes());
        }
    }

    private static class LockGuard implements AutoCloseable {
        private final Lock lock;
        public LockGuard(Lock lock) {
            this.lock = lock;
            lock.lock();
        }

        @Override
        public void close() {
            lock.unlock();
        }
    }
}