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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
package org.openslx.bwlp.sat;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.openslx.bwlp.sat.database.Updater;
import org.openslx.bwlp.sat.database.mappers.DbConfiguration;
import org.openslx.bwlp.sat.database.mappers.DbUser;
import org.openslx.bwlp.sat.fileserv.FileServer;
import org.openslx.bwlp.sat.maintenance.DeleteOldImages;
import org.openslx.bwlp.sat.maintenance.DeleteOldLectures;
import org.openslx.bwlp.sat.maintenance.DeleteOldUsers;
import org.openslx.bwlp.sat.maintenance.MailFlusher;
import org.openslx.bwlp.sat.maintenance.SendExpireWarning;
import org.openslx.bwlp.sat.thrift.BinaryListener;
import org.openslx.bwlp.sat.thrift.JsonHttpListener;
import org.openslx.bwlp.sat.thrift.cache.OperatingSystemList;
import org.openslx.bwlp.sat.thrift.cache.OrganizationList;
import org.openslx.bwlp.sat.thrift.cache.VirtualizerList;
import org.openslx.bwlp.sat.util.Configuration;
import org.openslx.bwlp.sat.util.Identity;
import org.openslx.bwlp.sat.web.WebServer;
import org.openslx.bwlp.thrift.iface.TAuthorizationException;
import org.openslx.bwlp.thrift.iface.TInvalidTokenException;
import org.openslx.sat.thrift.version.Version;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.thrifthelper.ThriftManager.ErrorCallback;
import org.openslx.util.AppUtil;
import org.openslx.util.CascadedThreadPoolExecutor;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;
public class App {
private static Logger LOGGER = LogManager.getLogger(App.class);
private static final String NAME = "bwLehrpool-Server";
private static final Set<String> failFastMethods = new HashSet<>();
public static void main(String[] args)
throws TTransportException, NoSuchAlgorithmException, IOException, KeyManagementException {
System.setProperty("mariadb.logging.disable", "true");
// setup basic logging appender to log output on console if no external appender (log4j.properties) is configured
if (org.apache.logging.log4j.core.Logger.class.cast(LogManager.getRootLogger())
.getAppenders()
.isEmpty()) {
Configurator.initialize(new DefaultConfiguration());
}
// get Configuration
try {
LOGGER.info("Loading configuration");
Configuration.load();
} catch (Exception e1) {
LOGGER.fatal("Could not load configuration", e1);
System.exit(1);
}
AppUtil.logHeader(LOGGER, App.NAME, App.class.getPackage().getImplementationVersion());
AppUtil.logProperty(LOGGER, "rpc.version", Long.toString(Version.VERSION));
AppUtil.logProperty(LOGGER, "server.features", SupportedFeatures.getFeatureString());
// Update database schema if applicable
try {
Updater.updateDatabase();
RuntimeConfig.get();
DbConfiguration.updateMailTemplates(false);
} catch (SQLException e1) {
LOGGER.fatal("Updating/checking the database layout failed.");
return;
}
if (Identity.loadCertificate() == null) {
LOGGER.error("Could not set up TLS/SSL requirements, exiting");
System.exit(1);
}
failFastMethods.add("getVirtualizers");
failFastMethods.add("getOperatingSystems");
failFastMethods.add("getOrganizations");
ThriftManager.setMasterErrorCallback(new ErrorCallback() {
/**
* Return true to retry call, false otherwise.
*/
@Override
public boolean thriftError(int failCount, String method, Throwable t) {
// t == null means no connection could be established
if (failFastMethods.contains(method))
return t != null && failCount <= 1;
if (failCount > 2 || t == null) {
LOGGER.warn("Thrift Client error for " + method + ", FAIL.");
return false;
}
if ((t instanceof TInvalidTokenException)
|| (t instanceof TAuthorizationException)
|| !(t instanceof TException)) {
return false;
}
if (((TException) t).getCause() == null) {
LOGGER.info("Thrift error " + t.toString() + " for " + method + ", retrying...");
} else {
LOGGER.info("Thrift error " + ((TException) t).getCause().toString() + " for " + method
+ ", retrying...");
}
try {
Thread.sleep(failCount * 250);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return true;
}
});
SSLContext ctx = null;
if (Configuration.getMasterServerSsl()) {
ctx = Configuration.getMasterServerSslContext();
}
ThriftManager.setMasterServerAddress(ctx, Configuration.getMasterServerAddress(),
Configuration.getMasterServerPort(), 30000);
// Load useful things from master server
if (VirtualizerList.get() == null || VirtualizerList.get().isEmpty()) {
LOGGER.fatal("Could not get initial virtualizer list from master server."
+ " Please make sure this server can connect to the internet.");
return;
}
if (OrganizationList.get() == null || OrganizationList.get().isEmpty()) {
LOGGER.fatal("Could not get initial organization list from master server."
+ " Please make sure this server can connect to the internet.");
return;
}
if (OperatingSystemList.get() == null || OperatingSystemList.get().isEmpty()) {
LOGGER.fatal("Could not get initial operating system list from master server."
+ " Please make sure this server can connect to the internet.");
return;
}
// Update once an hour
QuickTimer.scheduleAtFixedDelay(new Task() {
public void fire() {
VirtualizerList.get();
OperatingSystemList.get();
OrganizationList.get();
}
}, 3601_000, 3601_000 * 3);
// Start file transfer server
if (!FileServer.instance().start()) {
LOGGER.error("Could not start internal file server.");
return;
}
// Start watch dog to ensure nobody else is messing with the vmstore
QuickTimer.scheduleAtFixedDelay(new StorageUseCheck(), 10_000, 60_000);
// Set a flag that we need to convert userids if applicable
DbUser.checkIfLegacyUsersExist();
// Set up maintenance tasks
DeleteOldImages.init();
SendExpireWarning.init();
MailFlusher.init();
DeleteOldLectures.init();
DeleteOldUsers.init();
// Start Thrift Server
Thread t;
ThreadPoolExecutor tpe;
// Plain
tpe = new CascadedThreadPoolExecutor(1, 8, 1, TimeUnit.MINUTES,
new SynchronousQueue<Runnable>(), // Persistent connections, don't queue before growing
"Thr-Plain");
t = new Thread(new BinaryListener(9090, false, tpe), "Thr-Plain:9090");
t.setDaemon(true);
t.start();
// SSL
tpe = new CascadedThreadPoolExecutor(2, 8, 1, TimeUnit.MINUTES,
new SynchronousQueue<Runnable>(), // Persistent connections, don't queue before growing
"Thr-SSL");
t = new Thread(new BinaryListener(9091, true, tpe), "Thr-SSL:9091");
t.start();
// Start RPC httpd
tpe = new CascadedThreadPoolExecutor(2, 16, 1, TimeUnit.MINUTES,
new SynchronousQueue<Runnable>(), // Semi-persistent connections, better don't queue either
"RPC-http");
t = new Thread(new WebServer(9080, tpe), "RPC-httpd:9080");
t.setDaemon(true);
t.start();
// Start JSON-Thrift httpd
tpe = new CascadedThreadPoolExecutor(6, 12, 1, TimeUnit.MINUTES, 3, "Thr-http");
tpe.allowCoreThreadTimeOut(true);
t = new Thread(new JsonHttpListener(9081, tpe), "Thr-httpd:9081");
t.setDaemon(true);
t.start();
Runtime.getRuntime().addShutdownHook(new Thread("Shutdown") {
@Override
public void run() {
QuickTimer.cancel();
LOGGER.info("All services and workers shut down, exiting...");
}
});
}
}
|