-
Notifications
You must be signed in to change notification settings - Fork 13
/
Host.java
189 lines (163 loc) · 5.65 KB
/
Host.java
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
// Copyright (c) 2020-2021 VMware, Inc. or its affiliates. All rights reserved.
//
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// info@rabbitmq.com.
package com.rabbitmq.stream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;
public class Host {
public static final String DOCKER_PREFIX = "DOCKER:";
private static String capture(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder buff = new StringBuilder();
while ((line = br.readLine()) != null) {
buff.append(line).append("\n");
}
return buff.toString();
}
private static Process executeCommand(String command) throws IOException {
Process pr = executeCommandProcess(command);
int ev = waitForExitValue(pr);
if (ev != 0) {
String stdout = capture(pr.getInputStream());
String stderr = capture(pr.getErrorStream());
throw new IOException(
"unexpected command exit value: "
+ ev
+ "\ncommand: "
+ command
+ "\n"
+ "\nstdout:\n"
+ stdout
+ "\nstderr:\n"
+ stderr
+ "\n");
}
return pr;
}
public static String hostname() throws IOException {
Process process = executeCommand("hostname");
return capture(process.getInputStream()).trim();
}
private static int waitForExitValue(Process pr) {
while (true) {
try {
pr.waitFor();
break;
} catch (InterruptedException ignored) {
}
}
return pr.exitValue();
}
private static Process executeCommandProcess(String command) throws IOException {
String[] finalCommand;
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
finalCommand = new String[4];
finalCommand[0] = "C:\\winnt\\system32\\cmd.exe";
finalCommand[1] = "/y";
finalCommand[2] = "/c";
finalCommand[3] = command;
} else {
finalCommand = new String[3];
finalCommand[0] = "/bin/sh";
finalCommand[1] = "-c";
finalCommand[2] = command;
}
return Runtime.getRuntime().exec(finalCommand);
}
public static Process rabbitmqctl(String command) throws IOException {
return executeCommand(rabbitmqctlCommand() + " " + command);
}
public static Process killConnection(String connectionName) throws IOException {
return rabbitmqctl("eval 'rabbit_stream:kill_connection(\"" + connectionName + "\").'");
}
public static Process killStreamLeaderProcess(String stream) throws IOException {
return rabbitmqctl(
"eval 'case rabbit_stream_manager:lookup_leader(<<\"/\">>, <<\""
+ stream
+ "\">>) of {ok, Pid} -> exit(Pid, kill); Pid -> exit(Pid, kill) end.'");
}
public static String rabbitmqctlCommand() {
String rabbitmqCtl = System.getProperty("rabbitmqctl.bin");
if (rabbitmqCtl == null) {
throw new IllegalStateException("Please define the rabbitmqctl.bin system property");
}
if (rabbitmqCtl.startsWith(DOCKER_PREFIX)) {
String containerId = rabbitmqCtl.split(":")[1];
return "docker exec " + containerId + " rabbitmqctl";
} else {
return rabbitmqCtl;
}
}
public static AutoCloseable diskAlarm() throws Exception {
return new CallableAutoCloseable(
() -> {
setDiskAlarm();
return null;
},
() -> {
clearDiskAlarm();
return null;
});
}
public static AutoCloseable memoryAlarm() throws Exception {
return new CallableAutoCloseable(
() -> {
setMemoryAlarm();
return null;
},
() -> {
clearMemoryAlarm();
return null;
});
}
private static void setDiskAlarm() throws IOException {
setResourceAlarm("disk");
}
private static void clearDiskAlarm() throws IOException {
clearResourceAlarm("disk");
}
private static void setMemoryAlarm() throws IOException {
setResourceAlarm("memory");
}
private static void clearMemoryAlarm() throws IOException {
clearResourceAlarm("memory");
}
private static void setResourceAlarm(String source) throws IOException {
rabbitmqctl("eval 'rabbit_alarm:set_alarm({{resource_limit, " + source + ", node()}, []}).'");
}
private static void clearResourceAlarm(String source) throws IOException {
rabbitmqctl("eval 'rabbit_alarm:clear_alarm({resource_limit, " + source + ", node()}).'");
}
public static boolean isOnDocker() {
String rabbitmqCtl = System.getProperty("rabbitmqctl.bin");
if (rabbitmqCtl == null) {
throw new IllegalStateException("Please define the rabbitmqctl.bin system property");
}
return rabbitmqCtl.startsWith(DOCKER_PREFIX);
}
private static final class CallableAutoCloseable implements AutoCloseable {
private final Callable<Void> end;
private CallableAutoCloseable(Callable<Void> start, Callable<Void> end) throws Exception {
this.end = end;
start.call();
}
@Override
public void close() throws Exception {
this.end.call();
}
}
}