-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Karim TAAM <[email protected]>
- Loading branch information
Showing
10 changed files
with
309 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...perledger/besu/plugin/services/storage/rocksdb/segmented/KeyValueSnapshotTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
package org.hyperledger.besu.plugin.services.storage.rocksdb.segmented; | ||
|
||
import org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction; | ||
|
||
import java.util.Optional; | ||
|
||
/** The Rocks db snapshot transaction. */ | ||
public interface KeyValueSnapshotTransaction extends KeyValueStorageTransaction, AutoCloseable { | ||
|
||
/** | ||
* Get data against given key. | ||
* | ||
* @param key the key | ||
* @return the optional data | ||
*/ | ||
Optional<byte[]> get(final byte[] key); | ||
} |
117 changes: 117 additions & 0 deletions
117
...esu/plugin/services/storage/rocksdb/segmented/RocksDBColumnarKeyValueLayeredSnapshot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
package org.hyperledger.besu.plugin.services.storage.rocksdb.segmented; | ||
|
||
import static java.util.stream.Collectors.toUnmodifiableSet; | ||
|
||
import org.hyperledger.besu.plugin.services.exception.StorageException; | ||
import org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction; | ||
import org.hyperledger.besu.plugin.services.storage.SnappedKeyValueStorage; | ||
import org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBMetrics; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
/** The RocksDb columnar key value snapshot. */ | ||
public class RocksDBColumnarKeyValueLayeredSnapshot implements SnappedKeyValueStorage { | ||
|
||
private final RocksDBMetrics metrics; | ||
final RocksDBLayeredSnapshotTransaction snapTx; | ||
|
||
public RocksDBColumnarKeyValueLayeredSnapshot( | ||
final RocksDBMetrics metrics, final RocksDBLayeredSnapshotTransaction snapTx) { | ||
this.metrics = metrics; | ||
this.snapTx = snapTx.copy(); | ||
} | ||
|
||
public RocksDBColumnarKeyValueLayeredSnapshot( | ||
final RocksDBMetrics metrics, final RocksDBSnapshotTransaction snapTx) { | ||
this.metrics = metrics; | ||
this.snapTx = snapTx.copy(); | ||
} | ||
|
||
@Override | ||
public Optional<byte[]> get(final byte[] key) throws StorageException { | ||
return snapTx.get(key); | ||
} | ||
|
||
@Override | ||
public Stream<Pair<byte[], byte[]>> stream() { | ||
throw new UnsupportedOperationException("cannot stream"); | ||
} | ||
|
||
@Override | ||
public Stream<byte[]> streamKeys() { | ||
throw new UnsupportedOperationException("cannot stream"); | ||
} | ||
|
||
@Override | ||
public boolean tryDelete(final byte[] key) throws StorageException { | ||
snapTx.remove(key); | ||
return true; | ||
} | ||
|
||
@Override | ||
public Set<byte[]> getAllKeysThat(final Predicate<byte[]> returnCondition) { | ||
return streamKeys().filter(returnCondition).collect(toUnmodifiableSet()); | ||
} | ||
|
||
@Override | ||
public Set<byte[]> getAllValuesFromKeysThat(final Predicate<byte[]> returnCondition) { | ||
return stream() | ||
.filter(pair -> returnCondition.test(pair.getKey())) | ||
.map(Pair::getValue) | ||
.collect(toUnmodifiableSet()); | ||
} | ||
|
||
@Override | ||
public KeyValueStorageTransaction startTransaction() throws StorageException { | ||
// The use of a transaction on a transaction based key value store is dubious | ||
// at best. return our snapshot transaction instead. | ||
return snapTx; | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
throw new UnsupportedOperationException( | ||
"RocksDBColumnarKeyValueSnapshot does not support clear"); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(final byte[] key) throws StorageException { | ||
return snapTx.get(key).isPresent(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
snapTx.close(); | ||
} | ||
|
||
@Override | ||
public KeyValueStorageTransaction getSnapshotTransaction() { | ||
return snapTx; | ||
} | ||
|
||
@Override | ||
public SnappedKeyValueStorage cloneFromSnapshot() { | ||
return new RocksDBColumnarKeyValueLayeredSnapshot(metrics, snapTx); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...ger/besu/plugin/services/storage/rocksdb/segmented/RocksDBLayeredSnapshotTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
package org.hyperledger.besu.plugin.services.storage.rocksdb.segmented; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
import org.hyperledger.besu.plugin.services.exception.StorageException; | ||
import org.hyperledger.besu.plugin.services.metrics.OperationTimer; | ||
import org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction; | ||
import org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBMetrics; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** The Rocks db snapshot transaction. */ | ||
public class RocksDBLayeredSnapshotTransaction implements KeyValueSnapshotTransaction, AutoCloseable { | ||
private static final Logger LOG = LoggerFactory.getLogger(RocksDBLayeredSnapshotTransaction.class); | ||
|
||
private final AtomicBoolean isClosed = new AtomicBoolean(false); | ||
|
||
private final RocksDBMetrics metrics; | ||
|
||
private final KeyValueSnapshotTransaction parentSnapshot; | ||
private final Map<Bytes,Optional<byte[]>> inMemoryKeys; | ||
|
||
|
||
RocksDBLayeredSnapshotTransaction( | ||
final RocksDBMetrics metrics, | ||
final KeyValueSnapshotTransaction parentSnapshot) { | ||
this.metrics = metrics; | ||
this.parentSnapshot = parentSnapshot; | ||
this.inMemoryKeys = new ConcurrentHashMap<>(); | ||
} | ||
|
||
/** | ||
* Get data against given key. | ||
* | ||
* @param key the key | ||
* @return the optional data | ||
*/ | ||
@Override | ||
public Optional<byte[]> get(final byte[] key) { | ||
if (isClosed.get()) { | ||
LOG.debug("Attempted to access closed snapshot"); | ||
return Optional.empty(); | ||
} | ||
|
||
try (final OperationTimer.TimingContext ignored = metrics.getReadLatency().startTimer()) { | ||
return inMemoryKeys.getOrDefault(Bytes.of(key),parentSnapshot.get(key)); | ||
} | ||
} | ||
|
||
@Override | ||
public void put(final byte[] key, final byte[] value) { | ||
if (isClosed.get()) { | ||
LOG.debug("Attempted to access closed snapshot"); | ||
return; | ||
} | ||
try (final OperationTimer.TimingContext ignored = metrics.getWriteLatency().startTimer()) { | ||
inMemoryKeys.put(Bytes.of(key), Optional.ofNullable(value)); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove(final byte[] key) { | ||
if (isClosed.get()) { | ||
LOG.debug("Attempted to access closed snapshot"); | ||
return; | ||
} | ||
try (final OperationTimer.TimingContext ignored = metrics.getRemoveLatency().startTimer()) { | ||
inMemoryKeys.put(Bytes.of(key), Optional.empty()); | ||
} | ||
} | ||
|
||
|
||
public RocksDBLayeredSnapshotTransaction copy() { | ||
if (isClosed.get()) { | ||
throw new StorageException("Snapshot already closed"); | ||
} | ||
return new RocksDBLayeredSnapshotTransaction( | ||
metrics, this); | ||
} | ||
|
||
@Override | ||
public void commit() throws StorageException { | ||
// no op | ||
} | ||
|
||
@Override | ||
public void rollback() { | ||
inMemoryKeys.clear(); | ||
metrics.getRollbackCount().inc(); | ||
} | ||
|
||
|
||
@Override | ||
public void close() { | ||
inMemoryKeys.clear(); | ||
isClosed.set(true); | ||
} | ||
|
||
} |
Oops, something went wrong.