-
Notifications
You must be signed in to change notification settings - Fork 1.9k
IGNITE-28770 : Introduce a operation context attributes registry v2 #13243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vladsz83
wants to merge
32
commits into
apache:master
Choose a base branch
from
Vladsz83:IGNITE-28770-Introduce-a-operation-context-attributes-registry_v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+513
−191
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
1740273
impl
Vladsz83 77910fa
Merge branch 'master' into IGNITE-28770-Introduce-a-operation-context…
Vladsz83 baa1473
in-progress
Vladsz83 9f8e09d
raw
Vladsz83 72b5c17
Merge branch 'master' into IGNITE-28770-Introduce-a-operation-context…
Vladsz83 9f4ccec
impl
Vladsz83 c73b97c
Merge branch 'master' into IGNITE-28770-Introduce-a-operation-context…
Vladsz83 1987030
review fixes
Vladsz83 d293eb0
fix
Vladsz83 1395caf
review fixes
Vladsz83 79dab0b
reimpol
Vladsz83 695f18d
review fixes
Vladsz83 f0d579f
review fixes
Vladsz83 13d5f11
renaming
Vladsz83 61ef963
Merge branch 'master' into IGNITE-28770-Introduce-a-operation-context…
Vladsz83 1bc5fa8
review fixes
Vladsz83 cb024d1
fix
Vladsz83 4f5d180
fix
Vladsz83 a25c2f2
review fixes
Vladsz83 4af4ccd
typo
Vladsz83 f863cc4
typo
Vladsz83 c545034
txt
Vladsz83 8875351
fix
Vladsz83 41d5e1f
fix
Vladsz83 ec3b241
fix
Vladsz83 69b5916
fix
Vladsz83 2f1cf8d
fix
Vladsz83 33bb3a8
fix
Vladsz83 47aac3e
fix
Vladsz83 7d2778b
revert
Vladsz83 1858db5
review comment fix
Vladsz83 a0ed08a
minor style fix
Vladsz83 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
42 changes: 42 additions & 0 deletions
42
...les/core/src/main/java/org/apache/ignite/internal/DistributedOperationContextMessage.java
This file contains hidden or 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,42 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.ignite.internal; | ||
|
|
||
| import org.apache.ignite.internal.thread.context.DistributedOperationContextManager; | ||
| import org.apache.ignite.internal.thread.context.OperationContext; | ||
| import org.apache.ignite.plugin.extensions.communication.Message; | ||
|
|
||
| /** | ||
| * Transport for {@link OperationContext} distributed attributes. | ||
| * | ||
| * @see DistributedOperationContextManager | ||
| */ | ||
| public class DistributedOperationContextMessage implements Message { | ||
| /** Values of operation context attributes. */ | ||
| @Order(0) | ||
| public Message[] vals; | ||
|
|
||
| /** Bitmap of effective attributes ids. */ | ||
| @Order(1) | ||
| public byte idBitmap; | ||
|
|
||
| /** Empty constructor for serialization purposes. */ | ||
| public DistributedOperationContextMessage() { | ||
| // No-op. | ||
| } | ||
| } |
148 changes: 148 additions & 0 deletions
148
...in/java/org/apache/ignite/internal/thread/context/DistributedOperationContextManager.java
This file contains hidden or 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,148 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.ignite.internal.thread.context; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentSkipListMap; | ||
| import org.apache.ignite.IgniteException; | ||
| import org.apache.ignite.internal.DistributedOperationContextMessage; | ||
| import org.apache.ignite.internal.util.typedef.F; | ||
| import org.apache.ignite.plugin.extensions.communication.Message; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Provides the ability to manage {@link OperationContext} attributes in a distributed manner. | ||
| * | ||
| * <p>This mechanism is primarily used to propagate {@link OperationContext} state across the cluster by | ||
| * capturing it before a message is sent, transferring it together with the message, and restoring it on | ||
| * the receiving node before message processing begins.</p> | ||
| * | ||
| * <p>The implementation relies on a mapping between a distributed identifier and an | ||
| * {@link OperationContextAttribute} instance that is consistent across all cluster nodes.</p> | ||
| * | ||
| * <p>To enable propagation of an {@link OperationContextAttribute} value across cluster nodes, the | ||
| * attribute must be created using the {@link #createDistributedAttribute(byte, Message)} method. | ||
| * | ||
| * <p> Note, that the maximum number of distributed attribute instances that can be created is currently limited to | ||
| * {@link #MAX_DISTRIBUTED_ATTR_CNT} for implementation reasons.</p> | ||
| * | ||
| * @see OperationContext | ||
| * @see DistributedOperationContextMessage | ||
| */ | ||
| public class DistributedOperationContextManager { | ||
| /** */ | ||
| private static final DistributedOperationContextManager INSTANCE = new DistributedOperationContextManager(); | ||
|
|
||
| /** Maximal number of supported distributed attributes. */ | ||
| static final byte MAX_DISTRIBUTED_ATTR_CNT = Byte.SIZE; | ||
|
|
||
| /** Registered distributed attributes by their cluster-wide id. */ | ||
| private final Map<Byte, OperationContextAttribute<Message>> attrs = new ConcurrentSkipListMap<>(); | ||
|
|
||
| /** */ | ||
| public static DistributedOperationContextManager instance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link OperationContext} attribute with the specified distributed ID and initial value. | ||
| * | ||
| * <p>The distributed ID is used to consistently identify the attribute across all nodes in the cluster. | ||
| * It must be unique, and its value must be in the range from {@code 0} (inclusive) to {@code Byte.SIZE} (exclusive).</p> | ||
| * | ||
| * <p>The value of the created attribute is automatically captured and propagated between cluster nodes | ||
| * during message transmission.</p> | ||
| * | ||
| * @see OperationContextAttribute#newInstance(Object) | ||
| */ | ||
| public <T extends Message> OperationContextAttribute<T> createDistributedAttribute(byte id, @Nullable T initVal) { | ||
| assert id >= 0 && id < MAX_DISTRIBUTED_ATTR_CNT : "Invalid distributed attributed id [id=" + id + ']'; | ||
|
|
||
| return (OperationContextAttribute<T>)attrs.compute(id, (id0, attr0) -> { | ||
| if (attr0 != null) | ||
| throw new IgniteException("Duplicated distributed attribute id [id=" + id + ']'); | ||
|
|
||
| return OperationContextAttribute.newInstance(initVal); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Collects the values of all distributed {@link OperationContextAttribute}s registered by this manager in a format | ||
| * suitable for transmission between cluster nodes. | ||
| * | ||
| * @see OperationContext#get(OperationContextAttribute) | ||
| */ | ||
| public @Nullable DistributedOperationContextMessage collectDistributedAttributes() { | ||
| DistributedOperationContextMessage res = null; | ||
| List<Message> vals = null; | ||
|
|
||
| for (Map.Entry<Byte, OperationContextAttribute<Message>> e : attrs.entrySet()) { | ||
| OperationContextAttribute<? extends Message> attr = e.getValue(); | ||
|
|
||
| Message curVal = OperationContext.get(attr); | ||
|
|
||
| if (curVal != attr.initialValue()) { | ||
| if (res == null) { | ||
| res = new DistributedOperationContextMessage(); | ||
|
|
||
| vals = new ArrayList<>(MAX_DISTRIBUTED_ATTR_CNT / 2); | ||
| } | ||
|
|
||
| byte mask = (byte)(1 << e.getKey()); | ||
|
|
||
| assert (res.idBitmap & mask) == 0; | ||
|
|
||
| vals.add(curVal); | ||
| res.idBitmap |= mask; | ||
| } | ||
| } | ||
|
|
||
| if (res != null) | ||
| res.vals = vals.toArray(vals.toArray(new Message[vals.size()])); | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| /** Restores distributed {@link OperationContextAttribute} values received from a remote node. */ | ||
| public Scope restoreDistributedAttributes(@Nullable DistributedOperationContextMessage msg) { | ||
| if (msg == null) | ||
| return Scope.NOOP_SCOPE; | ||
|
|
||
| assert msg.idBitmap != 0; | ||
| assert !F.isEmpty(msg.vals); | ||
| assert msg.vals.length <= MAX_DISTRIBUTED_ATTR_CNT; | ||
|
|
||
| OperationContext.ContextUpdater updater = OperationContext.ContextUpdater.create(); | ||
|
|
||
| for (byte valIdx = 0, maskIdx = 0; valIdx < msg.vals.length; ++valIdx) { | ||
| Message curVal = msg.vals[valIdx]; | ||
|
|
||
| while ((msg.idBitmap & (1 << maskIdx)) == 0) | ||
| ++maskIdx; | ||
|
|
||
| OperationContextAttribute<Message> attr = attrs.get(maskIdx++); | ||
|
|
||
| assert attr != null; | ||
|
|
||
| updater.set(attr, curVal); | ||
| } | ||
|
|
||
| return updater.apply(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.