-
Notifications
You must be signed in to change notification settings - Fork 32
Fix MemQ sensor / zk issues #405
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
Merged
Merged
Changes from all commits
Commits
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
114 changes: 114 additions & 0 deletions
114
.../src/test/java/com/pinterest/orion/core/automation/sensor/memq/MemqClusterSensorTest.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,114 @@ | ||
| package com.pinterest.orion.core.automation.sensor.memq; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.pinterest.orion.core.Node; | ||
| import com.pinterest.orion.core.memq.MemqCluster; | ||
| import com.pinterest.orion.core.utils.memq.zookeeper.MemqZookeeperClient; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Verifies the defensive read behavior of {@link MemqClusterSensor}: cluster attributes are only | ||
| * published when a complete, trustworthy snapshot is read, so transient ZooKeeper failures never | ||
| * overwrite known-good state with empty / partial / cross-cluster data. | ||
| */ | ||
| public class MemqClusterSensorTest { | ||
|
|
||
| private MemqClusterSensor sensor; | ||
| private MemqCluster cluster; | ||
| private MemqZookeeperClient client; | ||
| private Map<String, Node> nodeMap; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| sensor = spy(new MemqClusterSensor()); | ||
| cluster = mock(MemqCluster.class); | ||
| client = mock(MemqZookeeperClient.class); | ||
| nodeMap = new HashMap<>(); | ||
| when(cluster.getClusterId()).thenReturn("scorpion01"); | ||
| when(cluster.getNodeMap()).thenReturn(nodeMap); | ||
| } | ||
|
|
||
| @Test | ||
| public void whenClientNotConnectedSkipsUpdate() throws Exception { | ||
| when(client.isConnected()).thenReturn(false); | ||
|
|
||
| sensor.sense(cluster, client); | ||
|
|
||
| verify(client, never()).getBrokerNames(); | ||
| verify(sensor, never()).setAttribute(any(), anyString(), any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void whenBrokerListReadFailsSkipsUpdate() throws Exception { | ||
| when(client.isConnected()).thenReturn(true); | ||
| when(client.getBrokerNames()).thenThrow(new RuntimeException("zk read failed")); | ||
|
|
||
| sensor.sense(cluster, client); | ||
|
|
||
| verify(sensor, never()).setAttribute(any(), anyString(), any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void whenBrokerDataReadFailsSkipsUpdate() throws Exception { | ||
| when(client.isConnected()).thenReturn(true); | ||
| when(client.getBrokerNames()).thenReturn(Arrays.asList("broker0")); | ||
| when(client.getBrokerData("broker0")).thenThrow(new RuntimeException("partial read")); | ||
|
|
||
| sensor.sense(cluster, client); | ||
|
|
||
| // Incomplete snapshot: must not read topics or publish anything. | ||
| verify(client, never()).getTopics(); | ||
| verify(sensor, never()).setAttribute(any(), anyString(), any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void whenTopicReadFailsSkipsUpdate() throws Exception { | ||
| when(client.isConnected()).thenReturn(true); | ||
| when(client.getBrokerNames()).thenReturn(Collections.emptyList()); | ||
| when(client.getTopics()).thenThrow(new RuntimeException("topic read failed")); | ||
|
|
||
| sensor.sense(cluster, client); | ||
|
|
||
| verify(sensor, never()).setAttribute(any(), anyString(), any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void whenNoBrokersPublishesNoBrokerContext() throws Exception { | ||
| when(client.isConnected()).thenReturn(true); | ||
| when(client.getBrokerNames()).thenReturn(Collections.emptyList()); | ||
| when(client.getTopics()).thenReturn(Collections.emptyList()); | ||
|
|
||
| sensor.sense(cluster, client); | ||
|
|
||
| verify(sensor).setAttribute(cluster, MemqCluster.CLUSTER_CONTEXT, "NO BROKER"); | ||
| } | ||
|
|
||
| @Test | ||
| public void whenBrokersPresentPublishesGovernorContext() throws Exception { | ||
| when(client.isConnected()).thenReturn(true); | ||
| when(client.getBrokerNames()).thenReturn(Arrays.asList("broker0")); | ||
| Broker broker = new Broker("10.0.0.1", (short) 9092, "c5.large", "us-east-1a", new HashSet<>()); | ||
| when(client.getBrokerData("broker0")).thenReturn(new Gson().toJson(broker)); | ||
| when(client.getTopics()).thenReturn(Collections.emptyList()); | ||
| when(client.getGovernorIp()).thenReturn("10.0.0.1"); | ||
|
|
||
| sensor.sense(cluster, client); | ||
|
|
||
| verify(sensor).setAttribute(cluster, MemqCluster.CLUSTER_CONTEXT, "Governor: 10.0.0.1\n"); | ||
| } | ||
| } |
Oops, something went wrong.
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.