blob: 1985fd534fbc90b7bb4909fc1c51a68108b17187 (
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 Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.filedistribution;
import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.config.FileReference;
import com.yahoo.config.subscription.ConfigSourceSet;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Transport;
import com.yahoo.net.HostName;
import com.yahoo.vespa.config.Connection;
import com.yahoo.vespa.config.ConnectionPool;
import com.yahoo.vespa.config.JRTConnectionPool;
import com.yahoo.vespa.config.server.ConfigServerSpec;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Utilities related to file distribution on config servers.
*
* @author musum
* @author gjoranv
*/
public class FileDistributionUtil {
/**
* Returns all files in the given directory, non-recursive.
*/
public static Set<String> getFileReferencesOnDisk(File directory) {
Set<String> fileReferencesOnDisk = new HashSet<>();
File[] filesOnDisk = directory.listFiles();
if (filesOnDisk != null)
fileReferencesOnDisk.addAll(Arrays.stream(filesOnDisk).map(File::getName).collect(Collectors.toSet()));
return fileReferencesOnDisk;
}
/**
* Returns a connection pool with all config servers except this one, or an empty pool if there
* is only one config server (no point in trying to download from yourself).
*/
public static ConnectionPool createConnectionPool(ConfigserverConfig configserverConfig) {
List<String> configServers = ConfigServerSpec.fromConfig(configserverConfig)
.stream()
.filter(spec -> !spec.getHostName().equals(HostName.getLocalhost()))
.map(spec -> "tcp/" + spec.getHostName() + ":" + spec.getConfigServerPort())
.collect(Collectors.toList());
return configServers.size() > 0
? new JRTConnectionPool(new ConfigSourceSet(configServers), "filedistribution-jrt-pool")
: emptyConnectionPool();
}
public static boolean fileReferenceExistsOnDisk(File downloadDirectory, FileReference applicationPackageReference) {
return getFileReferencesOnDisk(downloadDirectory).contains(applicationPackageReference.value());
}
static ConnectionPool emptyConnectionPool() {
return new EmptyConnectionPool();
}
private static class EmptyConnectionPool implements ConnectionPool {
private Supervisor supervisor;
@Override
public void close() {
synchronized (this) {
if (supervisor != null) {
supervisor.transport().shutdown().join();
}
}
}
@Override
public void setError(Connection connection, int i) {}
@Override
public Connection getCurrent() { return null; }
@Override
public Connection switchConnection(Connection connection) { return null; }
@Override
public int getSize() { return 0; }
@Override
public Supervisor getSupervisor() {
synchronized (this) {
if (supervisor == null) {
supervisor = new Supervisor(new Transport("empty-connectionpool"));
}
}
return supervisor;
}
}
}
|