forked from opentable/otj-pg-embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmbeddedPostgres.java
607 lines (537 loc) · 22.3 KB
/
EmbeddedPostgres.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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* 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 static com.google.common.base.MoreObjects.firstNonNull;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
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 java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.sql.DataSource;
import com.google.common.base.Joiner;
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 org.apache.commons.codec.binary.Hex;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.postgresql.ds.PGSimpleDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tukaani.xz.XZInputStream;
public class EmbeddedPostgres implements Closeable
{
private static final Logger LOG = LoggerFactory.getLogger(EmbeddedPostgres.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 LOCK_FILE_NAME = "epg-lock";
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 final File pgDir;
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;
EmbeddedPostgres(File parentDirectory, File dataDirectory, boolean cleanDataDirectory, Map<String, String> postgresConfig, int port, PgBinaryResolver pgBinaryResolver) throws IOException
{
this.cleanDataDirectory = cleanDataDirectory;
this.postgresConfig = ImmutableMap.copyOf(postgresConfig);
this.port = port;
this.pgDir = prepareBinaries(pgBinaryResolver);
if (parentDirectory != null) {
mkdirs(parentDirectory);
cleanOldDataDirectories(parentDirectory);
this.dataDirectory = 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 PGSimpleDataSource ds = new PGSimpleDataSource();
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
{
try (final ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
}
}
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("pg_ctl"),
"-D", dataDirectory.getPath(),
"-o", Joiner.on(" ").join(createInitOptions()),
"start"
);
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 List<String> createInitOptions()
{
final List<String> initOptions = Lists.newArrayList(
"-p", Integer.toString(port),
"-i", "-F"
);
for (final Entry<String, String> config : postgresConfig.entrySet())
{
initOptions.add("-c");
initOptions.add(config.getKey() + "=" + config.getValue());
}
return initOptions;
}
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 {
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(EmbeddedPostgres.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);
final 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);
}
} 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 String pgBin(String binaryName)
{
final String extension = SystemUtils.IS_OS_WINDOWS ? ".exe" : "";
return new File(pgDir, "bin/" + binaryName + extension).getPath();
}
public static EmbeddedPostgres start() throws IOException
{
return builder().start();
}
public static EmbeddedPostgres.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;
private PgBinaryResolver pgBinaryResolver = new BundledPostgresBinaryResolver();
Builder() {
config.put("timezone", "UTC");
config.put("synchronous_commit", "off");
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 Builder setPgBinaryResolver(PgBinaryResolver pgBinaryResolver) {
this.pgBinaryResolver = pgBinaryResolver;
return this;
}
public EmbeddedPostgres start() throws IOException
{
if (builderPort == 0)
{
builderPort = detectPort();
}
return new EmbeddedPostgres(parentDirectory, builderDataDirectory, builderCleanDataDirectory, config, builderPort, pgBinaryResolver);
}
}
private static List<String> system(String... command)
{
try {
final ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
final Process process = builder.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);
}
private static final AtomicReference<File> BINARY_DIR = new AtomicReference<>();
private static final Lock PREPARE_BINARIES_LOCK = new ReentrantLock();
/**
* Get current operating system string. The string is used in the appropriate postgres binary name.
*
* @return Current operating system string.
*/
private static String getOS()
{
if (SystemUtils.IS_OS_WINDOWS) {
return "Windows";
}
if (SystemUtils.IS_OS_MAC_OSX) {
return "Darwin";
}
if (SystemUtils.IS_OS_LINUX) {
return "Linux";
}
throw new UnsupportedOperationException("Unknown OS " + SystemUtils.OS_NAME);
}
/**
* Get the machine architecture string. The string is used in the appropriate postgres binary name.
*
* @return Current machine architecture string.
*/
private static String getArchitecture()
{
return "amd64".equals(SystemUtils.OS_ARCH) ? "x86_64" : SystemUtils.OS_ARCH;
}
/**
* Unpack archive compressed by tar with bzip2 compression. By default system tar is used (faster). If not found, then the
* java implementation takes place.
*
* @param tbzPath The archive path.
* @param targetDir The directory to extract the content to.
*/
private static void extractTxz(String tbzPath, String targetDir) throws IOException
{
try (
InputStream in = Files.newInputStream(Paths.get(tbzPath));
XZInputStream xzIn = new XZInputStream(in);
TarArchiveInputStream tarIn = new TarArchiveInputStream(xzIn)
) {
TarArchiveEntry entry;
while ((entry = tarIn.getNextTarEntry()) != null) {
final String individualFile = entry.getName();
final File fsObject = new File(targetDir + "/" + individualFile);
if (entry.isSymbolicLink()) {
Path target = FileSystems.getDefault().getPath(entry.getLinkName());
Files.createSymbolicLink(fsObject.toPath(), target);
} else if (entry.isFile()) {
byte[] content = new byte[(int) entry.getSize()];
int read = tarIn.read(content, 0, content.length);
Preconditions.checkState(read != -1, "could not read %s", individualFile);
mkdirs(fsObject.getParentFile());
try (OutputStream outputFile = new FileOutputStream(fsObject)) {
IOUtils.write(content, outputFile);
}
} else if (entry.isDirectory()) {
mkdirs(fsObject);
} else {
throw new UnsupportedOperationException(
String.format("Unsupported entry found: %s", individualFile)
);
}
if (individualFile.startsWith("bin/")) {
fsObject.setExecutable(true);
}
}
}
}
private static File prepareBinaries(PgBinaryResolver pgBinaryResolver)
{
PREPARE_BINARIES_LOCK.lock();
try {
if(BINARY_DIR.get() != null) {
return BINARY_DIR.get();
}
final String system = getOS();
final String machineHardware = getArchitecture();
LOG.info("Detected a {} {} system", system, machineHardware);
File pgDir;
File pgTbz;
try {
pgTbz = File.createTempFile("pgpg", "pgpg");
} catch (final IOException e) {
throw new ExceptionInInitializerError(e);
}
try (final DigestInputStream pgArchiveData = new DigestInputStream(
pgBinaryResolver.getPgBinary(system, machineHardware),
MessageDigest.getInstance("MD5"));
final FileOutputStream os = new FileOutputStream(pgTbz))
{
IOUtils.copy(pgArchiveData, os);
pgArchiveData.close();
os.close();
String pgDigest = Hex.encodeHexString(pgArchiveData.getMessageDigest().digest());
pgDir = new File(TMP_DIR, String.format("PG-%s", pgDigest));
mkdirs(pgDir);
final File unpackLockFile = new File(pgDir, LOCK_FILE_NAME);
final File pgDirExists = new File(pgDir, ".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...");
extractTxz(pgTbz.getPath(), pgDir.getPath());
Preconditions.checkState(pgDirExists.createNewFile(), "couldn't make .exists file");
} catch (Exception e) {
LOG.error("while unpacking Postgres", e);
}
} 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!");
}
} finally {
if (unpackLockFile.exists()) {
Preconditions.checkState(unpackLockFile.delete(), "could not remove lock file %s", unpackLockFile.getAbsolutePath());
}
}
}
} 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);
}
BINARY_DIR.set(pgDir);
LOG.info("Postgres binaries at {}", pgDir);
return pgDir;
} finally {
PREPARE_BINARIES_LOCK.unlock();
}
}
@Override
public String toString()
{
return "EmbeddedPG-" + instanceId;
}
}