Skip to content

Commit

Permalink
Merge branch 'release/0.48' into 12356-register-metrics-in-virtual-map
Browse files Browse the repository at this point in the history
* release/0.48:
  chore: Migration fixes from state dumpers (#12392)
  fix: Clean up leaked virtual maps after migration (#12411)
  fix: Fix signing requirements for updating metadata on Token (#12409)
  feat: Utilization metrics (#12402)
  fix: Fix to deterministic iteration order (#12401)
  fix: (mono) reclaim entity id after failed auto-create; avoid `FAIL_INVALID` on invalid token id (#12322)
  chore: rely on protobuf release 48 branch instead of main (#12376)
  fix: fixed diff test issue 12355. (#12375)
  fix: ContractCallLocal INSUFFICIENT_GAS ERROR (#12362)
  fix: disable birth rounds (#12351)

# Conflicts:
#	hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java
#	hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/merkle/MerkleHederaState.java
  • Loading branch information
netopyr committed Mar 28, 2024
2 parents 67460d7 + cdafd54 commit 2b41a1c
Show file tree
Hide file tree
Showing 121 changed files with 1,385 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ static List<DifferingEntries> diff(
"Additional modular record found at "
+ secondEntries.get(i).consensusTime() + " for transactionID : "
+ secondEntries.get(i).txnRecord().getTransactionID() + " transBody : "
+ secondEntries.get(i).body()));
+ secondEntries.get(i).body()
+ "\n -> \n" + secondEntries.get(i).txnRecord()));
continue;
}
final var secondEntry = entryWithMatchableRecord(secondEntries, i, firstEntry);
Expand Down
2 changes: 1 addition & 1 deletion hedera-node/hapi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ description = "Hedera API"

// Add downloaded HAPI repo protobuf files into build directory and add to sources to build them
tasks.cloneHederaProtobufs {
branchOrTag = "main"
branchOrTag = "v0.48.3-release"
// As long as the 'branchOrTag' above is not stable, run always:
outputs.upToDateWhen { false }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.hedera.node.app.spi.state.WritableStates;
import com.swirlds.config.api.Configuration;
import com.swirlds.metrics.api.Metrics;
import edu.umd.cs.findbugs.annotations.NonNull;

/**
Expand All @@ -37,8 +38,10 @@ public interface ServiceApiProvider<T> {
* Creates a new instance of the service API.
*
* @param configuration the node configuration
* @param metrics the metrics API
* @param writableStates the writable state of the service
* @return the new API instance
*/
T newInstance(@NonNull Configuration configuration, @NonNull WritableStates writableStates);
T newInstance(
@NonNull Configuration configuration, @NonNull Metrics metrics, @NonNull WritableStates writableStates);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hedera.node.app.spi.state;

import com.swirlds.metrics.api.Metrics;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Iterator;
import java.util.Objects;
Expand Down Expand Up @@ -80,9 +81,14 @@ protected Iterator<K> iterateFromDataSource() {
}

/** {@inheritDoc} */
@NonNull
@Override
public long sizeOfDataSource() {
return delegate.size();
}

/** {@inheritDoc} */
@Override
public void setupMetrics(@NonNull Metrics metrics, @NonNull String name, @NonNull String label, long maxCapacity) {
delegate.setupMetrics(metrics, name, label, maxCapacity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hedera.node.app.spi.state;

import com.swirlds.metrics.api.Metrics;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Iterator;
Expand Down Expand Up @@ -109,4 +110,32 @@ public interface WritableKVState<K, V> extends ReadableKVState<K, V> {
default boolean isModified() {
return !modifiedKeys().isEmpty();
}

/**
* Sets up metrics for the {@code WritableKVState}.
*
* <p>This is an intermediate solution until we are sure the data layer is reporting the right values.
* The default implementation is empty which means that no metrics are set up.
*
* @param metrics the metrics-API used to report utilization
* @param name the name of the entities
* @param label the label of the entities
* @param maxCapacity the maximum capacity of the store
*/
default void setupMetrics(
@NonNull Metrics metrics, @NonNull String name, @NonNull String label, long maxCapacity) {}

/**
* Sets up metrics for the {@code WritableKVState}.
*
* <p>This is an intermediate solution until we are sure the data layer is reporting the right values.
* The default implementation is empty which means that no metrics are set up.
*
* @param metrics the metrics-API used to report utilization
* @param name the name of the entities
* @param maxCapacity the maximum capacity of the store
*/
default void setupMetrics(@NonNull Metrics metrics, @NonNull String name, long maxCapacity) {
setupMetrics(metrics, name, name, maxCapacity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected WritableKVStateBase(@NonNull final String stateKey) {
* be called by the code that created the {@link WritableKVStateBase} instance or owns it. Don't
* cast and commit unless you own the instance!
*/
public final void commit() {
public void commit() {
for (final var entry : modifications.entrySet()) {
final var key = entry.getKey();
final var value = entry.getValue();
Expand Down
1 change: 1 addition & 0 deletions hedera-node/hedera-app-spi/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
requires transitive com.hedera.node.hapi;
requires transitive com.hedera.pbj.runtime;
requires transitive com.swirlds.config.api;
requires transitive com.swirlds.metrics.api;
requires static com.github.spotbugs.annotations;

exports com.hedera.node.app.spi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -231,6 +232,12 @@ public final class Hedera implements SwirldMain {
private static FeeService FEE_SERVICE;
private static CongestionThrottleService CONGESTION_THROTTLE_SERVICE;

/**
* In order to migrate from mono to modular, we need to keep references to the virtual maps that we are migrating.
* Then they can be properly closed when the migration is complete.
*/
private static final Set<VirtualMap<?, ?>> MONO_VIRTUAL_MAPS = ConcurrentHashMap.newKeySet();

/*==================================================================================================================
*
* Hedera Object Construction.
Expand Down Expand Up @@ -446,7 +453,6 @@ public void onStateInitialized(
dumpMonoChildrenFrom(state, MONO_PRE_MIGRATION);
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Failed to dump mono state before migration at MONO_PRE_MIGRATION", e);
}

Expand All @@ -459,7 +465,8 @@ public void onStateInitialized(
// Copy this virtual map, so it doesn't get released before the migration is done
final var copy = uniqTokensFromState.copy();
copy.registerMetrics(metrics);
TOKEN_SERVICE.setNftsFromState(copy);
MONO_VIRTUAL_MAPS.add(copy);
TOKEN_SERVICE.setNftsFromState(uniqTokensFromState);
}

// --------------------- TOKEN_ASSOCIATIONS (1)
Expand All @@ -469,7 +476,8 @@ public void onStateInitialized(
// Copy this virtual map, so it doesn't get released before the migration is done
final var copy = tokenRelsFromState.copy();
copy.registerMetrics(metrics);
TOKEN_SERVICE.setTokenRelsFromState(copy);
MONO_VIRTUAL_MAPS.add(copy);
TOKEN_SERVICE.setTokenRelsFromState(tokenRelsFromState);
}

// --------------------- TOPICS (2)
Expand All @@ -484,20 +492,23 @@ public void onStateInitialized(
// Copy this virtual map, so it doesn't get released before the migration is done
final var copy = filesFromState.copy();
copy.registerMetrics(metrics);
FILE_SERVICE.setFs(() -> VirtualMapLike.from(copy));
MONO_VIRTUAL_MAPS.add(copy);

// Note: some files have no metadata, e.g. contract bytecode files
FILE_SERVICE.setFs(() -> VirtualMapLike.from(filesFromState));

// We also need to make this available to the contract service, so it can extract contract bytecode
CONTRACT_SERVICE.setBytecodeFromState(() -> VirtualMapLike.from(copy));
CONTRACT_SERVICE.setBytecodeFromState(() -> VirtualMapLike.from(filesFromState));
}
// Note: some files have no metadata, e.g. contract bytecode files

// --------------------- ACCOUNTS (4)
final VirtualMap<EntityNumVirtualKey, OnDiskAccount> acctsFromState = state.getChild(ACCOUNTS);
if (acctsFromState != null) {
// Copy this virtual map, so it doesn't get released before the migration is done
final var copy = acctsFromState.copy();
copy.registerMetrics(metrics);
TOKEN_SERVICE.setAcctsFromState(copy);
MONO_VIRTUAL_MAPS.add(copy);
TOKEN_SERVICE.setAcctsFromState(acctsFromState);
}

// --------------------- TOKENS (5)
Expand All @@ -510,8 +521,6 @@ public void onStateInitialized(
// Here we assign the network context, but don't migrate it by itself. These properties have been split out
// to various services in the modular code, and will each be migrated in its appropriate service.
final MerkleNetworkContext fromNetworkContext = state.getChild(NETWORK_CTX);
// the translator is using firstConsTimeOfLastBlock instead of CURRENTBlock...is that ok???
// firstConsTimeOfCurrentBlock – needed in blockInfo

// --------------------- SPECIAL_FILES (7)
// No longer useful; don't migrate
Expand All @@ -523,7 +532,6 @@ public void onStateInitialized(
}

// --------------------- RECORD_STREAM_RUNNING_HASH (9)
// From MerkleNetworkContext: blockNo, blockHashes
final RecordsRunningHashLeaf blockInfoFromState = state.getChild(RECORD_STREAM_RUNNING_HASH);
if (blockInfoFromState != null) {
BLOCK_SERVICE.setFs(blockInfoFromState, fromNetworkContext);
Expand All @@ -538,7 +546,8 @@ public void onStateInitialized(
// Copy this virtual map, so it doesn't get released before the migration is done
final var copy = contractFromStorage.copy();
copy.registerMetrics(metrics);
CONTRACT_SERVICE.setStorageFromState(VirtualMapLike.from(copy));
MONO_VIRTUAL_MAPS.add(copy);
CONTRACT_SERVICE.setStorageFromState(VirtualMapLike.from(contractFromStorage));
}

// --------------------- STAKING_INFO (12)
Expand All @@ -565,8 +574,9 @@ public void onStateInitialized(

// --------------------- CONGESTION THROTTLE SERVICE (14)
if (fromNetworkContext != null) {
CONGESTION_THROTTLE_SERVICE.setFs(fromNetworkContext);
InitialModServiceAdminSchema.setFs(fromNetworkContext);
CONGESTION_THROTTLE_SERVICE.setFs(
fromNetworkContext.usageSnapshots(), fromNetworkContext.getGasThrottleUsageSnapshot());
InitialModServiceAdminSchema.setFs(true);
}

// Here we release all mono children so that we don't have a bunch of null routes in state
Expand Down Expand Up @@ -637,6 +647,7 @@ public void onStateInitialized(
platformState.getFreezeTime(),
platformState.getLastFrozenTime());
}

/**
* Called by this class when we detect it is time to do migration. The {@code deserializedVersion} must not be newer
* than the current software version. If it is prior to the current version, then each migration between the
Expand Down Expand Up @@ -671,12 +682,20 @@ private void onMigrate(
logger.info("Migration versions are {} to {}", previousVersion, currentVersion);
migrator.doMigrations(
state, currentVersion, previousVersion, configProvider.getConfiguration(), networkInfo, metrics);

// Now that migrations are complete, clean up the leftover virtual maps
MONO_VIRTUAL_MAPS.forEach(vm -> {
if (!vm.isDestroyed()) {
vm.release();
}
});
MONO_VIRTUAL_MAPS.clear();

try {
if (shouldDump(trigger, MOD_POST_MIGRATION)) {
dumpModChildrenFrom(state, MOD_POST_MIGRATION);
}
} catch (Exception t) {
t.printStackTrace();
logger.error("Error dumping state after migration at MOD_POST_MIGRATION", t);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ private static Bytes getBlockHashes(FCQueue<BytesElement> queue) throws IOExcept
ByteArrayOutputStream collector = new ByteArrayOutputStream();
final var iterator = queue.iterator();
while (iterator.hasNext()) {
final byte[] hashes = new byte[48];
final var element = iterator.next();
collector.write(element.getData());
System.arraycopy(element.getData(), 0, hashes, 0, 32);
collector.write(hashes);
}
return Bytes.wrap(collector.toByteArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public class MerkleHederaState extends PartialNaryMerkleInternal implements Merk
*/
private final HederaLifecycles lifecycles;

public Map<String, Map<String, StateMetadata<?, ?>>> getServices() {
return services;
}

private Metrics metrics;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.hedera.node.app.spi.state.WritableKVState;
import com.hedera.node.app.spi.state.WritableKVStateBase;
import com.hedera.node.app.state.merkle.StateMetadata;
import com.swirlds.metrics.api.Metrics;
import com.swirlds.virtualmap.VirtualMap;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Iterator;
Expand All @@ -39,6 +40,8 @@ public final class OnDiskWritableKVState<K, V> extends WritableKVStateBase<K, V>

private final StateMetadata<K, V> md;

private OnDiskWritableKvStateMetrics kvStateMetrics;

/**
* Create a new instance
*
Expand Down Expand Up @@ -112,4 +115,18 @@ public long sizeOfDataSource() {
logMapGetSize(getStateKey(), size);
return size;
}

@Override
public void setupMetrics(@NonNull Metrics metrics, @NonNull String name, @NonNull String label, long maxCapacity) {
kvStateMetrics = new OnDiskWritableKvStateMetrics(metrics, name, label, sizeOfDataSource(), maxCapacity);
}

@Override
public void commit() {
super.commit();

if (kvStateMetrics != null) {
kvStateMetrics.updateMetrics(sizeOfDataSource());
}
}
}
Loading

0 comments on commit 2b41a1c

Please sign in to comment.