-
Notifications
You must be signed in to change notification settings - Fork 144
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: Michael Heinrichs <[email protected]>
- Loading branch information
Showing
22 changed files
with
483 additions
and
35 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
46 changes: 46 additions & 0 deletions
46
...ra-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/workflows/WarmupContext.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,46 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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.hedera.node.app.spi.workflows; | ||
|
||
import com.hedera.hapi.node.transaction.TransactionBody; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
/** | ||
* Represents the context of a single {@code warm()}-call. | ||
*/ | ||
public interface WarmupContext { | ||
|
||
/** | ||
* Gets the {@link TransactionBody} | ||
* | ||
* @return the {@link TransactionBody} in this context | ||
*/ | ||
@NonNull | ||
TransactionBody body(); | ||
|
||
/** | ||
* Create a new store given the store's interface. This gives read-only access to the store. | ||
* | ||
* @param storeInterface The store interface to find and create a store for | ||
* @param <C> Interface class for a Store | ||
* @return An implementation of store interface provided, or null if the store | ||
* @throws IllegalArgumentException if the storeInterface class provided is unknown to the app | ||
* @throws NullPointerException if {@code storeInterface} is {@code null} | ||
*/ | ||
@NonNull | ||
<C> C createStore(@NonNull final Class<C> storeInterface); | ||
} |
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
107 changes: 107 additions & 0 deletions
107
hedera-node/hedera-app/src/main/java/com/hedera/node/app/workflows/handle/CacheWarmer.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,107 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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.hedera.node.app.workflows.handle; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.hedera.hapi.node.transaction.TransactionBody; | ||
import com.hedera.node.app.spi.workflows.PreCheckException; | ||
import com.hedera.node.app.spi.workflows.TransactionHandler; | ||
import com.hedera.node.app.state.HederaState; | ||
import com.hedera.node.app.workflows.TransactionChecker; | ||
import com.hedera.node.app.workflows.dispatcher.ReadableStoreFactory; | ||
import com.hedera.node.app.workflows.dispatcher.TransactionDispatcher; | ||
import com.hedera.node.app.workflows.prehandle.PreHandleResult; | ||
import com.hedera.pbj.runtime.io.buffer.Bytes; | ||
import com.swirlds.platform.system.Round; | ||
import com.swirlds.platform.system.events.ConsensusEvent; | ||
import com.swirlds.platform.system.transaction.Transaction; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ForkJoinPool; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
/** | ||
* This class is used to warm up the cache. It is called at the beginning of a round with the current state | ||
* and the round. It will start a background thread which iterates through all transactions and calls the | ||
* {@link TransactionHandler#warm} method. | ||
*/ | ||
@Singleton | ||
public class CacheWarmer { | ||
|
||
private final TransactionChecker checker; | ||
private final TransactionDispatcher dispatcher; | ||
private final Executor executor; | ||
|
||
@Inject | ||
public CacheWarmer(@NonNull final TransactionChecker checker, @NonNull final TransactionDispatcher dispatcher) { | ||
this(checker, dispatcher, ForkJoinPool.commonPool()); | ||
} | ||
|
||
@VisibleForTesting | ||
CacheWarmer( | ||
@NonNull final TransactionChecker checker, | ||
@NonNull final TransactionDispatcher dispatcher, | ||
@NonNull final Executor executor) { | ||
this.checker = checker; | ||
this.dispatcher = requireNonNull(dispatcher); | ||
this.executor = requireNonNull(executor); | ||
} | ||
|
||
/** | ||
* Warms up the cache for the given round. | ||
* | ||
* @param state the current state | ||
* @param round the current round | ||
*/ | ||
public void warm(@NonNull final HederaState state, @NonNull final Round round) { | ||
executor.execute(() -> { | ||
final ReadableStoreFactory storeFactory = new ReadableStoreFactory(state); | ||
for (final ConsensusEvent event : round) { | ||
event.forEachTransaction(platformTransaction -> executor.execute(() -> { | ||
final TransactionBody txBody = extractTransactionBody(platformTransaction); | ||
if (txBody != null) { | ||
final var context = new WarmupContextImpl(txBody, storeFactory); | ||
dispatcher.dispatchWarmup(context); | ||
} | ||
})); | ||
} | ||
}); | ||
} | ||
|
||
@Nullable | ||
private TransactionBody extractTransactionBody(@NonNull final Transaction platformTransaction) { | ||
// First we check if the transaction was already parsed during pre-handle (should be almost always the case) | ||
final var metadata = platformTransaction.getMetadata(); | ||
if (metadata instanceof PreHandleResult result) { | ||
return result.txInfo() == null ? null : result.txInfo().txBody(); | ||
} | ||
|
||
// If not we parse it here using existing code. This is not ideal but should be rare. | ||
// We can potentially optimize this by limiting the code to the bare minimum needed | ||
// or keeping the result for later. | ||
try { | ||
final Bytes buffer = Bytes.wrap(platformTransaction.getContents()); | ||
return checker.parseAndCheck(buffer).txBody(); | ||
} catch (PreCheckException ex) { | ||
return null; | ||
} | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
...node/hedera-app/src/main/java/com/hedera/node/app/workflows/handle/WarmupContextImpl.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,58 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* 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.hedera.node.app.workflows.handle; | ||
|
||
import com.hedera.hapi.node.transaction.TransactionBody; | ||
import com.hedera.node.app.spi.workflows.WarmupContext; | ||
import com.hedera.node.app.workflows.TransactionInfo; | ||
import com.hedera.node.app.workflows.dispatcher.ReadableStoreFactory; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
/** | ||
* The default implementation of {@link WarmupContext}. | ||
*/ | ||
public class WarmupContextImpl implements WarmupContext { | ||
|
||
@NonNull | ||
private final TransactionBody txBody; | ||
|
||
@NonNull | ||
private final ReadableStoreFactory storeFactory; | ||
|
||
/** | ||
* Constructor of {@code WarmupContextImpl} | ||
* | ||
* @param txBody the {@link TransactionInfo} of the transaction | ||
* @param storeFactory the {@link ReadableStoreFactory} to create stores | ||
*/ | ||
public WarmupContextImpl(@NonNull final TransactionBody txBody, @NonNull final ReadableStoreFactory storeFactory) { | ||
this.txBody = txBody; | ||
this.storeFactory = storeFactory; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public TransactionBody body() { | ||
return txBody; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public <C> C createStore(@NonNull final Class<C> storeInterface) { | ||
return storeFactory.getStore(storeInterface); | ||
} | ||
} |
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
Oops, something went wrong.