forked from opentable/otj-pg-embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbeddedPostgreSQL.java
More file actions
503 lines (440 loc) · 18.1 KB
/
EmbeddedPostgreSQL.java
File metadata and controls
503 lines (440 loc) · 18.1 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.opentable.db.postgres.embedded;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.sql.DataSource;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Closeables;
import com.google.common.io.Files;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.postgresql.jdbc2.optional.SimpleDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EmbeddedPostgreSQL implements Closeable
{
private static final Logger LOG = LoggerFactory.getLogger(EmbeddedPostgreSQL.class);
private static final String JDBC_FORMAT = "jdbc:postgresql://localhost:%s/%s?user=%s";
private static final String PG_STOP_MODE = "fast";
private static final String PG_STOP_WAIT_S = "5";
private static final String PG_SUPERUSER = "postgres";
private static final int PG_STARTUP_WAIT_MS = 10 * 1000;
private static final String PG_DIGEST;
private static final String TMP_DIR_LOC = System.getProperty("java.io.tmpdir");
private static final File TMP_DIR = new File(TMP_DIR_LOC, "embedded-pg");
private static final String LOCK_FILE_NAME = "epg-lock";
private static final String UNAME_S, UNAME_M;
private static final File PG_DIR;
private final File dataDirectory, lockFile;
private final UUID instanceId = UUID.randomUUID();
private final int port;
private final AtomicBoolean started = new AtomicBoolean();
private final AtomicBoolean closed = new AtomicBoolean();
private final Map<String, String> postgresConfig;
private volatile Process postmaster;
private volatile FileOutputStream lockStream;
private volatile FileLock lock;
private final boolean cleanDataDirectory;
EmbeddedPostgreSQL(File parentDirectory, File dataDirectory, boolean cleanDataDirectory, Map<String, String> postgresConfig, int port) throws IOException
{
this.cleanDataDirectory = cleanDataDirectory;
this.postgresConfig = ImmutableMap.copyOf(postgresConfig);
this.port = port;
if (parentDirectory != null) {
mkdirs(parentDirectory);
cleanOldDataDirectories(parentDirectory);
this.dataDirectory = MoreObjects.firstNonNull(dataDirectory, new File(parentDirectory, instanceId.toString()));
} else {
this.dataDirectory = dataDirectory;
}
Preconditions.checkArgument(this.dataDirectory != null, "null data directory");
LOG.trace("{} postgres data directory is {}", instanceId, this.dataDirectory);
Preconditions.checkState(this.dataDirectory.exists() || this.dataDirectory.mkdir(), "Failed to mkdir %s", this.dataDirectory);
lockFile = new File(this.dataDirectory, LOCK_FILE_NAME);
if (cleanDataDirectory || !new File(dataDirectory, "postgresql.conf").exists()) {
initdb();
}
lock();
startPostmaster();
}
public DataSource getTemplateDatabase()
{
return getDatabase("postgres", "template1");
}
public DataSource getPostgresDatabase()
{
return getDatabase("postgres", "postgres");
}
public DataSource getDatabase(String userName, String dbName)
{
final SimpleDataSource ds = new SimpleDataSource();
ds.setServerName("localhost");
ds.setPortNumber(port);
ds.setDatabaseName(dbName);
ds.setUser(userName);
return ds;
}
public String getJdbcUrl(String userName, String dbName)
{
return String.format(JDBC_FORMAT, port, dbName, userName);
}
public int getPort()
{
return port;
}
private static int detectPort() throws IOException
{
final ServerSocket socket = new ServerSocket(0);
try {
return socket.getLocalPort();
} finally {
socket.close();
}
}
private void lock() throws IOException
{
lockStream = new FileOutputStream(lockFile);
Preconditions.checkState((lock = lockStream.getChannel().tryLock()) != null, "could not lock %s", lockFile);
}
private void initdb()
{
final StopWatch watch = new StopWatch();
watch.start();
system(pgBin("initdb"), "-A", "trust", "-U", PG_SUPERUSER, "-D", dataDirectory.getPath(), "-E", "UTF-8");
LOG.info("{} initdb completed in {}", instanceId, watch);
}
private void startPostmaster() throws IOException
{
final StopWatch watch = new StopWatch();
watch.start();
Preconditions.checkState(started.getAndSet(true) == false, "Postmaster already started");
final List<String> args = Lists.newArrayList(
pgBin("postgres"),
"-D", dataDirectory.getPath(),
"-p", Integer.toString(port),
"-i",
"-F");
for (final Entry<String, String> config : postgresConfig.entrySet())
{
args.add("-c");
args.add(config.getKey() + "=" + config.getValue());
}
final ProcessBuilder builder = new ProcessBuilder(args);
builder.redirectErrorStream(true);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
postmaster = builder.start();
LOG.info("{} postmaster started as {} on port {}. Waiting up to {}ms for server startup to finish.", instanceId, postmaster.toString(), port, PG_STARTUP_WAIT_MS);
Runtime.getRuntime().addShutdownHook(newCloserThread());
waitForServerStartup(watch);
}
private void waitForServerStartup(StopWatch watch) throws UnknownHostException, IOException
{
Throwable lastCause = null;
final long start = System.nanoTime();
final long maxWaitNs = TimeUnit.NANOSECONDS.convert(PG_STARTUP_WAIT_MS, TimeUnit.MILLISECONDS);
while (System.nanoTime() - start < maxWaitNs) {
try {
checkReady();
LOG.info("{} postmaster startup finished in {}", instanceId, watch);
return;
} catch (final SQLException e) {
lastCause = e;
LOG.trace("While waiting for server startup", e);
}
try {
throw new IOException(String.format("%s postmaster exited with value %d, check standard out for more detail!", instanceId, postmaster.exitValue()));
} catch (final IllegalThreadStateException e) {
// Process is not yet dead, loop and try again
LOG.trace("While waiting for server startup", e);
}
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
throw new IOException("Gave up waiting for server to start after " + PG_STARTUP_WAIT_MS + "ms", lastCause);
}
private void checkReady() throws SQLException
{
try (final Connection c = getPostgresDatabase().getConnection()) {
try (final Statement s = c.createStatement()) {
try (final ResultSet rs = s.executeQuery("SELECT 1")) { // NOPMD
Preconditions.checkState(rs.next() == true, "expecting single row");
Preconditions.checkState(1 == rs.getInt(1), "expecting 1");
Preconditions.checkState(rs.next() == false, "expecting single row");
}
}
}
}
private Thread newCloserThread()
{
final Thread closeThread = new Thread(new Runnable() {
@Override
public void run()
{
try {
Closeables.close(EmbeddedPostgreSQL.this, true);
}
catch (IOException ex) {
LOG.error("Unexpected IOException from Closeables.close", ex);
}
}
});
closeThread.setName("postgres-" + instanceId + "-closer");
return closeThread;
}
@Override
public void close() throws IOException
{
if (closed.getAndSet(true)) {
return;
}
final StopWatch watch = new StopWatch();
watch.start();
try {
pgCtl(dataDirectory, "stop");
LOG.info("{} shut down postmaster in {}", instanceId, watch);
} catch (final Exception e) {
LOG.error("Could not stop postmaster " + instanceId, e);
}
if (lock != null) {
lock.release();
}
Closeables.close(lockStream, true);
if (cleanDataDirectory && System.getProperty("ot.epg.no-cleanup") == null) {
try {
FileUtils.deleteDirectory(dataDirectory);
} catch (IOException e) {
LOG.error("Could not clean up directory {}", dataDirectory.getAbsolutePath());
}
} else {
LOG.info("Did not clean up directory {}", dataDirectory.getAbsolutePath());
}
}
private void pgCtl(File dir, String action)
{
system(pgBin("pg_ctl"), "-D", dir.getPath(), action, "-m", PG_STOP_MODE, "-t", PG_STOP_WAIT_S, "-w");
}
private void cleanOldDataDirectories(File parentDirectory)
{
final File[] children = parentDirectory.listFiles();
if (children == null) {
return;
}
for (final File dir : children)
{
if (!dir.isDirectory()) {
continue;
}
final File lockFile = new File(dir, LOCK_FILE_NAME);
final boolean isTooNew = System.currentTimeMillis() - lockFile.lastModified() < 10 * 60 * 1000;
if (!lockFile.exists() || isTooNew) {
continue;
}
try {
final FileOutputStream fos = new FileOutputStream(lockFile);
try {
try (FileLock lock = fos.getChannel().tryLock()) {
if (lock != null) {
LOG.info("Found stale data directory {}", dir);
if (new File(dir, "postmaster.pid").exists()) {
try {
pgCtl(dir, "stop");
LOG.info("Shut down orphaned postmaster!");
} catch (Exception e) {
if (LOG.isDebugEnabled()) {
LOG.warn("Failed to stop postmaster " + dir, e);
} else {
LOG.warn("Failed to stop postmaster " + dir + ": " + e.getMessage());
}
}
}
FileUtils.deleteDirectory(dir);
}
}
} finally {
fos.close();
}
} catch (final OverlappingFileLockException e) {
// The directory belongs to another instance in this VM.
LOG.trace("While cleaning old data directories", e);
} catch (final Exception e) {
LOG.warn("While cleaning old data directories", e);
}
}
}
private static String pgBin(String binaryName)
{
return new File(PG_DIR, "bin/" + binaryName).getPath();
}
public static EmbeddedPostgreSQL start() throws IOException
{
return builder().start();
}
public static EmbeddedPostgreSQL.Builder builder()
{
return new Builder();
}
public static class Builder
{
private final File parentDirectory = new File(System.getProperty("ness.embedded-pg.dir", TMP_DIR.getPath()));
private File builderDataDirectory;
private final Map<String, String> config = Maps.newHashMap();
private boolean builderCleanDataDirectory = true;
private int builderPort = 0;
Builder() {
config.put("timezone", "UTC");
config.put("synchronous_commit", "off");
config.put("checkpoint_segments", "64");
config.put("max_connections", "300");
}
public Builder setCleanDataDirectory(boolean cleanDataDirectory)
{
builderCleanDataDirectory = cleanDataDirectory;
return this;
}
public Builder setDataDirectory(File directory)
{
builderDataDirectory = directory;
return this;
}
public Builder setServerConfig(String key, String value)
{
config.put(key, value);
return this;
}
public Builder setPort(int port)
{
builderPort = port;
return this;
}
public EmbeddedPostgreSQL start() throws IOException
{
if (builderPort == 0)
{
builderPort = detectPort();
}
return new EmbeddedPostgreSQL(parentDirectory, builderDataDirectory, builderCleanDataDirectory, config, builderPort);
}
}
private static List<String> system(String... command)
{
try {
final Process process = new ProcessBuilder(command).start();
Preconditions.checkState(0 == process.waitFor(), "Process %s failed\n%s", Arrays.asList(command), IOUtils.toString(process.getErrorStream()));
try (InputStream stream = process.getInputStream()) {
return IOUtils.readLines(stream);
}
} catch (final Exception e) {
throw Throwables.propagate(e);
}
}
private static void mkdirs(File dir)
{
Preconditions.checkState(dir.mkdirs() || (dir.isDirectory() && dir.exists()), // NOPMD
"could not create %s", dir);
}
static {
UNAME_S = system("uname", "-s").get(0);
UNAME_M = system("uname", "-m").get(0);
LOG.info("Detected a {} {} system", UNAME_S, UNAME_M);
File pgTbz;
try {
pgTbz = File.createTempFile("pgpg", "pgpg");
} catch (final IOException e) {
throw new ExceptionInInitializerError(e);
}
try {
final DigestInputStream pgArchiveData = new DigestInputStream(
EmbeddedPostgreSQL.class.getResourceAsStream(String.format("/postgresql-%s-%s.tbz", UNAME_S, UNAME_M)),
MessageDigest.getInstance("MD5"));
final FileOutputStream os = new FileOutputStream(pgTbz);
IOUtils.copy(pgArchiveData, os);
pgArchiveData.close();
os.close();
PG_DIGEST = Hex.encodeHexString(pgArchiveData.getMessageDigest().digest());
PG_DIR = new File(TMP_DIR, String.format("PG-%s", PG_DIGEST));
mkdirs(PG_DIR);
final File unpackLockFile = new File(PG_DIR, LOCK_FILE_NAME);
final File pgDirExists = new File(PG_DIR, ".exists");
if (!pgDirExists.exists()) {
try (final FileOutputStream lockStream = new FileOutputStream(unpackLockFile);
final FileLock unpackLock = lockStream.getChannel().tryLock()) {
if (unpackLock != null) {
try {
Preconditions.checkState(!pgDirExists.exists(), "unpack lock acquired but .exists file is present.");
LOG.info("Extracting Postgres...");
system("tar", "-x", "-f", pgTbz.getPath(), "-C", PG_DIR.getPath());
Files.touch(pgDirExists);
} finally {
Preconditions.checkState(unpackLockFile.delete(), "could not remove lock file %s", unpackLockFile.getAbsolutePath());
}
} else {
// the other guy is unpacking for us.
int maxAttempts = 60;
while (!pgDirExists.exists() && --maxAttempts > 0) {
Thread.sleep(1000L);
}
Preconditions.checkState(pgDirExists.exists(), "Waited 60 seconds for postgres to be unpacked but it never finished!");
}
}
}
} catch (final IOException | NoSuchAlgorithmException e) {
throw new ExceptionInInitializerError(e);
} catch (final InterruptedException ie) {
Thread.currentThread().interrupt();
throw new ExceptionInInitializerError(ie);
} finally {
Preconditions.checkState(pgTbz.delete(), "could not delete %s", pgTbz);
}
LOG.info("Postgres binaries at {}", PG_DIR);
}
@Override
public String toString()
{
return "EmbeddedPG-" + instanceId;
}
}