-
Notifications
You must be signed in to change notification settings - Fork 575
refactor(store): fix reflection parameter error and extract duplicate methods to RaftReflectionUtil #2906
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
refactor(store): fix reflection parameter error and extract duplicate methods to RaftReflectionUtil #2906
Changes from all commits
732df8c
ee5f52d
79eb69e
f16c97d
b3250cd
d90faf0
d43a8f6
82300c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,95 @@ | ||||||
| /* | ||||||
| * 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.hugegraph.pd.raft; | ||||||
|
|
||||||
| import com.alipay.sofa.jraft.Node; | ||||||
| import com.alipay.sofa.jraft.ReplicatorGroup; | ||||||
| import com.alipay.sofa.jraft.core.Replicator; | ||||||
| import com.alipay.sofa.jraft.entity.PeerId; | ||||||
| import com.alipay.sofa.jraft.util.ThreadId; | ||||||
|
|
||||||
| import lombok.extern.slf4j.Slf4j; | ||||||
|
|
||||||
| @Slf4j | ||||||
imbajin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| public class RaftReflectionUtil { | ||||||
|
|
||||||
| public static Replicator.State getReplicatorState(Node node, PeerId peerId) { | ||||||
| if (node == null || peerId == null) { | ||||||
imbajin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| // Get ReplicatorGroup from Node | ||||||
| var clz = node.getClass(); | ||||||
| ReplicatorGroup replicateGroup = null; | ||||||
| try { | ||||||
| var f = clz.getDeclaredField("replicatorGroup"); | ||||||
| f.setAccessible(true); | ||||||
| try { | ||||||
| replicateGroup = (ReplicatorGroup)f.get(node); | ||||||
| } | ||||||
| finally { | ||||||
| f.setAccessible(false); | ||||||
| } | ||||||
| } | ||||||
| catch (NoSuchFieldException | IllegalAccessException e) { | ||||||
| log.warn("Failed to get replicator state via reflection: {}", e.getMessage(), e); | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| if (replicateGroup == null) { | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| ThreadId threadId = replicateGroup.getReplicator(peerId); | ||||||
| if (threadId == null) { | ||||||
| return null; | ||||||
| } | ||||||
| else { | ||||||
| Replicator r = (Replicator)threadId.lock(); | ||||||
| try { | ||||||
| if (r == null) { | ||||||
| return Replicator.State.Probe; | ||||||
| } | ||||||
| Replicator.State result = null; | ||||||
|
|
||||||
| // Get state from Replicator | ||||||
|
|
||||||
| var replicatorClz = r.getClass(); | ||||||
| try { | ||||||
| var f = replicatorClz.getDeclaredField("state"); | ||||||
| f.setAccessible(true); | ||||||
| try { | ||||||
| result = (Replicator.State)f.get(r); | ||||||
imbajin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| }catch (Exception e){ | ||||||
| log.warn("Failed to get replicator state for peerId: {}, error: {}", peerId, e.getMessage()); | ||||||
| } | ||||||
| finally { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 新代码正确地使用了 try-finally 块来确保:
这是一个很好的改进,提高了代码的健壮性和安全性。 建议:考虑在异常处理时记录更详细的上下文信息(如 peerId),便于问题排查:
Suggested change
|
||||||
| f.setAccessible(false); | ||||||
| } | ||||||
| } | ||||||
| catch (NoSuchFieldException e) { | ||||||
| log.warn("Failed to get replicator state via reflection: {}", e.getMessage(), e); | ||||||
| result = null; | ||||||
| } | ||||||
| return result; | ||||||
| } finally { | ||||||
| threadId.unlock(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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.hugegraph.pd.raft; | ||
|
|
||
| import com.alipay.sofa.jraft.Node; | ||
| import com.alipay.sofa.jraft.core.Replicator; | ||
| import com.alipay.sofa.jraft.entity.PeerId; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| public class RaftReflectionUtilTest { | ||
|
|
||
| @Test | ||
| public void testGetReplicatorStateWithNullNode() { | ||
| // Setup | ||
| PeerId peerId = mock(PeerId.class); | ||
|
|
||
| // Run the test | ||
| Replicator.State result = RaftReflectionUtil.getReplicatorState(null, peerId); | ||
|
|
||
| // Verify the results | ||
| Assert.assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetReplicatorStateWithNullPeerId() { | ||
| // Setup | ||
| Node node = mock(Node.class); | ||
|
|
||
| // Run the test | ||
| Replicator.State result = RaftReflectionUtil.getReplicatorState(node, null); | ||
|
|
||
| // Verify the results | ||
| Assert.assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetReplicatorStateWithBothNull() { | ||
| // Run the test | ||
| Replicator.State result = RaftReflectionUtil.getReplicatorState(null, null); | ||
|
|
||
| // Verify the results | ||
| Assert.assertNull(result); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -178,6 +178,11 @@ | |
| <artifactId>hg-store-client</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.hugegraph</groupId> | ||
| <artifactId>hg-pd-core</artifactId> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在 hg-store-core 的 pom.xml 中添加了对 hg-pd-core 的编译时依赖: <dependency>
<groupId>org.apache.hugegraph</groupId>
<artifactId>hg-pd-core</artifactId>
<version>1.7.0</version>
<scope>compile</scope>
</dependency>潜在问题:
建议:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
嗯嗯, 合理的, 另外 revision 应该是不需要声明的, 参考一下 server 的父子模块, 它默认会使用项目级别的版本号 (revision) |
||
| <version>${revision}</version> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <plugins> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.