Search in sources :

Example 1 with GFSnapshotImporter

use of org.apache.geode.internal.cache.snapshot.GFSnapshot.GFSnapshotImporter in project geode by apache.

the class RegionSnapshotServiceImpl method importOnMember.

private void importOnMember(File snapshot, SnapshotFormat format, SnapshotOptions<K, V> options) throws IOException, ClassNotFoundException {
    final LocalRegion local = getLocalRegion(region);
    if (getLoggerI18n().infoEnabled())
        getLoggerI18n().info(LocalizedStrings.Snapshot_IMPORT_BEGIN_0, region.getName());
    long count = 0;
    long bytes = 0;
    long start = CachePerfStats.getStatTime();
    // Would be interesting to use a PriorityQueue ordered on isDone()
    // but this is probably close enough in practice.
    LinkedList<Future<?>> puts = new LinkedList<Future<?>>();
    GFSnapshotImporter in = new GFSnapshotImporter(snapshot);
    try {
        int bufferSize = 0;
        Map<K, V> buffer = new HashMap<K, V>();
        SnapshotRecord record;
        while ((record = in.readSnapshotRecord()) != null) {
            bytes += record.getSize();
            K key = record.getKeyObject();
            // Until we modify the semantics of put/putAll to allow null values we
            // have to subvert the API by using Token.INVALID. Alternatively we could
            // invoke create/invalidate directly but that prevents us from using
            // bulk operations. The ugly type coercion below is necessary to allow
            // strong typing elsewhere.
            V val = (V) Token.INVALID;
            if (record.hasValue()) {
                byte[] data = record.getValue();
                // get lost and we start seeing serialization problems.
                if (data.length > 0 && data[0] == DSCODE.BYTE_ARRAY) {
                    // It would be faster to use System.arraycopy() directly but since
                    // length field is variable it's probably safest and simplest to
                    // keep the logic in the InternalDataSerializer.
                    val = record.getValueObject();
                } else {
                    val = (V) CachedDeserializableFactory.create(record.getValue());
                }
            }
            if (includeEntry(options, key, val)) {
                buffer.put(key, val);
                bufferSize += record.getSize();
                count++;
                // can keep the disk busy. Throttle puts so we don't overwhelm the cache.
                if (bufferSize > BUFFER_SIZE) {
                    if (puts.size() == IMPORT_CONCURRENCY) {
                        puts.removeFirst().get();
                    }
                    final Map<K, V> copy = new HashMap<K, V>(buffer);
                    Future<?> f = GemFireCacheImpl.getExisting("Importing region from snapshot").getDistributionManager().getWaitingThreadPool().submit(new Runnable() {

                        @Override
                        public void run() {
                            local.basicImportPutAll(copy, !options.shouldInvokeCallbacks());
                        }
                    });
                    puts.addLast(f);
                    buffer.clear();
                    bufferSize = 0;
                }
            }
        }
        // send off any remaining entries
        if (!buffer.isEmpty()) {
            local.basicImportPutAll(buffer, !options.shouldInvokeCallbacks());
        }
        // wait for completion and check for errors
        while (!puts.isEmpty()) {
            puts.removeFirst().get();
        }
        if (getLoggerI18n().infoEnabled()) {
            getLoggerI18n().info(LocalizedStrings.Snapshot_IMPORT_END_0_1_2_3, new Object[] { count, bytes, region.getName(), snapshot });
        }
    } catch (InterruptedException e) {
        while (!puts.isEmpty()) {
            puts.removeFirst().cancel(true);
        }
        Thread.currentThread().interrupt();
        throw (IOException) new InterruptedIOException().initCause(e);
    } catch (ExecutionException e) {
        while (!puts.isEmpty()) {
            puts.removeFirst().cancel(true);
        }
        throw new IOException(e);
    } finally {
        in.close();
        local.getCachePerfStats().endImport(count, start);
    }
}
Also used : GFSnapshotImporter(org.apache.geode.internal.cache.snapshot.GFSnapshot.GFSnapshotImporter) HashMap(java.util.HashMap) SnapshotRecord(org.apache.geode.internal.cache.snapshot.SnapshotPacket.SnapshotRecord) LinkedList(java.util.LinkedList) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with GFSnapshotImporter

use of org.apache.geode.internal.cache.snapshot.GFSnapshot.GFSnapshotImporter in project geode by apache.

the class CacheSnapshotServiceImpl method load.

@Override
public void load(File[] snapshots, SnapshotFormat format, SnapshotOptions<Object, Object> options) throws IOException, ClassNotFoundException {
    for (File f : snapshots) {
        GFSnapshotImporter in = new GFSnapshotImporter(f);
        try {
            byte version = in.getVersion();
            if (version == GFSnapshot.SNAP_VER_1) {
                throw new IOException(LocalizedStrings.Snapshot_UNSUPPORTED_SNAPSHOT_VERSION_0.toLocalizedString(version));
            }
            String regionName = in.getRegionName();
            Region<Object, Object> region = cache.getRegion(regionName);
            if (region == null) {
                throw new RegionNotFoundException(LocalizedStrings.Snapshot_COULD_NOT_FIND_REGION_0_1.toLocalizedString(regionName, f));
            }
            RegionSnapshotService<Object, Object> rs = region.getSnapshotService();
            rs.load(f, format, options);
        } finally {
            in.close();
        }
    }
}
Also used : GFSnapshotImporter(org.apache.geode.internal.cache.snapshot.GFSnapshot.GFSnapshotImporter) RegionNotFoundException(org.apache.geode.admin.RegionNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 3 with GFSnapshotImporter

use of org.apache.geode.internal.cache.snapshot.GFSnapshot.GFSnapshotImporter in project geode by apache.

the class GFSnapshotJUnitPerformanceTest method testStreamReadPerformance.

@Test
public void testStreamReadPerformance() throws IOException, ClassNotFoundException {
    writeFile(100000, val);
    int i = 0;
    while (i++ < 10) {
        long start = System.currentTimeMillis();
        int count = 0;
        GFSnapshotImporter in = new GFSnapshotImporter(f);
        SnapshotRecord entry;
        while ((entry = in.readSnapshotRecord()) != null) {
            count++;
        }
        in.close();
        long elapsed = System.currentTimeMillis() - start;
        double rate = 1000.0 * count / elapsed;
        System.out.println(rate + " stream read operations / sec");
    }
}
Also used : GFSnapshotImporter(org.apache.geode.internal.cache.snapshot.GFSnapshot.GFSnapshotImporter) SnapshotRecord(org.apache.geode.internal.cache.snapshot.SnapshotPacket.SnapshotRecord) Test(org.junit.Test) PerformanceTest(org.apache.geode.test.junit.categories.PerformanceTest)

Aggregations

GFSnapshotImporter (org.apache.geode.internal.cache.snapshot.GFSnapshot.GFSnapshotImporter)3 SnapshotRecord (org.apache.geode.internal.cache.snapshot.SnapshotPacket.SnapshotRecord)2 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 RegionNotFoundException (org.apache.geode.admin.RegionNotFoundException)1 PerformanceTest (org.apache.geode.test.junit.categories.PerformanceTest)1 Test (org.junit.Test)1