Search in sources :

Example 6 with TransactionSnapshot

use of org.apache.tephra.persist.TransactionSnapshot in project cdap by caskdata.

the class TransactionManagerDebuggerMain method takeSnapshot.

/**
 * Take a snapshot from the CDAP transaction manager and retrieve it.
 * @return the transaction manager snapshot just taken
 */
private TransactionSnapshot takeSnapshot() {
    URL url;
    HttpURLConnection connection = null;
    try {
        url = new URL("http://" + hostname + ":" + portNumber + "/v3/transactions/state");
        connection = (HttpURLConnection) url.openConnection();
        if (accessToken != null) {
            connection.setRequestProperty("Authorization", "Bearer " + accessToken);
        }
        System.out.println("About to take a snapshot of the transaction manager at " + url.toURI() + ", timestamp is " + System.currentTimeMillis() + " ms");
        int responseCode = connection.getResponseCode();
        if (responseCode == 200) {
            // Retrieve and deserialize the snapshot
            TransactionSnapshot snapshot;
            try (InputStream input = connection.getInputStream()) {
                snapshot = codecProvider.decode(input);
            }
            System.out.println("Snapshot taken and retrieved properly, snapshot timestamp is " + snapshot.getTimestamp() + " ms");
            if (persistingFilename != null) {
                // Persist the snapshot on disk for future queries and debugging
                File outputFile = new File(persistingFilename);
                try (OutputStream out = new FileOutputStream(outputFile)) {
                    // todo use pipes here to avoid having everyhting in memory twice
                    codecProvider.encode(out, snapshot);
                }
                System.out.println("Snapshot persisted on your disk as " + outputFile.getAbsolutePath() + " for future queries.");
            } else {
                System.out.println("Persist option not activated - Snapshot won't be persisted on your disk.");
            }
            return snapshot;
        } else if (responseCode == 401) {
            readUnauthorizedError(connection);
        } else {
            System.out.println("Snapshot could not be taken. Error code: " + responseCode);
        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}
Also used : TransactionSnapshot(org.apache.tephra.persist.TransactionSnapshot) HttpURLConnection(java.net.HttpURLConnection) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.commons.cli.ParseException)

Example 7 with TransactionSnapshot

use of org.apache.tephra.persist.TransactionSnapshot in project cdap by caskdata.

the class TransactionManagerDebuggerMain method retrieveSnapshot.

/**
 * Retrieve a persisted snapshot taken in the past.
 * @return the decoded transaction manager snapshot
 */
private TransactionSnapshot retrieveSnapshot() {
    try {
        System.out.println("Retrieving snapshot from file " + existingFilename);
        File snapshotFile = new File(existingFilename);
        try (FileInputStream fis = new FileInputStream(snapshotFile)) {
            TransactionSnapshot snapshot = codecProvider.decode(fis);
            System.out.println("Snapshot retrieved, timestamp is " + snapshot.getTimestamp() + " ms.");
            return snapshot;
        }
    } catch (IOException e) {
        System.out.println("File " + existingFilename + " could not be read.");
        e.printStackTrace();
        return null;
    }
}
Also used : TransactionSnapshot(org.apache.tephra.persist.TransactionSnapshot) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 8 with TransactionSnapshot

use of org.apache.tephra.persist.TransactionSnapshot in project cdap by caskdata.

the class HBaseTableCoprocessorTestRun method setupBeforeClass.

@BeforeClass
public static void setupBeforeClass() throws Exception {
    hConf = HBASE_TEST_BASE.getConfiguration();
    hConf.set(HBaseTableUtil.CFG_HBASE_TABLE_COMPRESSION, HBaseTableUtil.CompressionType.NONE.name());
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().getAbsolutePath());
    cConf.set(Constants.CFG_HDFS_NAMESPACE, cConf.get(Constants.CFG_LOCAL_DATA_DIR));
    cConf.set(Constants.CFG_HDFS_USER, System.getProperty("user.name"));
    // Reduce the metadata cache refresh frequency for unit tests
    cConf.set(Constants.MessagingSystem.COPROCESSOR_METADATA_CACHE_UPDATE_FREQUENCY_SECONDS, Integer.toString(METADATA_CACHE_EXPIRY));
    hBaseAdmin = HBASE_TEST_BASE.getHBaseAdmin();
    hBaseAdmin.getConfiguration().set(HBaseTableUtil.CFG_HBASE_TABLE_COMPRESSION, HBaseTableUtil.CompressionType.NONE.name());
    tableUtil = new HBaseTableUtilFactory(cConf).get();
    ddlExecutor = new HBaseDDLExecutorFactory(cConf, hConf).get();
    ddlExecutor.createNamespaceIfNotExists(tableUtil.getHBaseNamespace(NamespaceId.SYSTEM));
    LocationFactory locationFactory = getInjector().getInstance(LocationFactory.class);
    tableFactory = new HBaseTableFactory(cConf, hBaseAdmin.getConfiguration(), tableUtil, locationFactory);
    new ConfigurationWriter(hConf, cConf).write(ConfigurationReader.Type.DEFAULT, cConf);
    // write an initial transaction snapshot
    invalidList.addAll(ImmutableList.of(V[3], V[5], V[7]));
    TransactionSnapshot txSnapshot = TransactionSnapshot.copyFrom(System.currentTimeMillis(), V[6] - 1, V[7], invalidList, // this will set visibility upper bound to V[6]
    Maps.newTreeMap(ImmutableSortedMap.of(V[6], new TransactionManager.InProgressTx(V[6] - 1, Long.MAX_VALUE, TransactionManager.InProgressType.SHORT))), new HashMap<Long, TransactionManager.ChangeSet>(), new TreeMap<Long, TransactionManager.ChangeSet>());
    HDFSTransactionStateStorage tmpStorage = new HDFSTransactionStateStorage(hConf, new SnapshotCodecProvider(hConf), new TxMetricsCollector());
    tmpStorage.startAndWait();
    tmpStorage.writeSnapshot(txSnapshot);
    tmpStorage.stopAndWait();
}
Also used : ConfigurationWriter(co.cask.cdap.data2.util.hbase.ConfigurationWriter) TxMetricsCollector(org.apache.tephra.metrics.TxMetricsCollector) SnapshotCodecProvider(org.apache.tephra.snapshot.SnapshotCodecProvider) LocationFactory(org.apache.twill.filesystem.LocationFactory) HDFSTransactionStateStorage(org.apache.tephra.persist.HDFSTransactionStateStorage) TransactionSnapshot(org.apache.tephra.persist.TransactionSnapshot) TransactionManager(org.apache.tephra.TransactionManager) HBaseDDLExecutorFactory(co.cask.cdap.data2.util.hbase.HBaseDDLExecutorFactory) HBaseTableUtilFactory(co.cask.cdap.data2.util.hbase.HBaseTableUtilFactory) BeforeClass(org.junit.BeforeClass)

Aggregations

TransactionSnapshot (org.apache.tephra.persist.TransactionSnapshot)8 IOException (java.io.IOException)4 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Transaction (org.apache.tephra.Transaction)2 BinaryDecoder (org.apache.tephra.snapshot.BinaryDecoder)2 SnapshotCodecProvider (org.apache.tephra.snapshot.SnapshotCodecProvider)2 Test (org.junit.Test)2 CConfiguration (co.cask.cdap.common.conf.CConfiguration)1 ConfigurationWriter (co.cask.cdap.data2.util.hbase.ConfigurationWriter)1 HBaseDDLExecutorFactory (co.cask.cdap.data2.util.hbase.HBaseDDLExecutorFactory)1 HBaseTableUtilFactory (co.cask.cdap.data2.util.hbase.HBaseTableUtilFactory)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 HashSet (java.util.HashSet)1