|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.cdm.job; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 20 | +import static org.mockito.ArgumentMatchers.contains; |
| 21 | +import static org.mockito.Mockito.*; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 28 | +import org.mockito.Mock; |
| 29 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 30 | +import org.slf4j.Logger; |
| 31 | + |
| 32 | +import com.datastax.cdm.cql.EnhancedSession; |
| 33 | +import com.datastax.cdm.properties.IPropertyHelper; |
| 34 | +import com.datastax.cdm.properties.KnownProperties; |
| 35 | +import com.datastax.cdm.properties.PropertyHelper; |
| 36 | +import com.datastax.cdm.schema.ClusterConfigurationException; |
| 37 | +import com.datastax.cdm.schema.CqlTable; |
| 38 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 39 | + |
| 40 | +@ExtendWith(MockitoExtension.class) |
| 41 | +public class AbstractJobSessionErrorHandlingTest { |
| 42 | + |
| 43 | + @Mock |
| 44 | + private CqlSession originSession; |
| 45 | + |
| 46 | + @Mock |
| 47 | + private CqlSession targetSession; |
| 48 | + |
| 49 | + @Mock |
| 50 | + private IPropertyHelper propertyHelper; |
| 51 | + |
| 52 | + @Mock |
| 53 | + private Logger mockLogger; |
| 54 | + |
| 55 | + // Create a concrete implementation of AbstractJobSession for testing |
| 56 | + private class TestJobSession extends AbstractJobSession<Object> { |
| 57 | + private IPropertyHelper iPropertyHelper; |
| 58 | + |
| 59 | + public TestJobSession(CqlSession originSession, CqlSession targetSession, IPropertyHelper propHelper) { |
| 60 | + // Use the getInstance method to get the PropertyHelper singleton |
| 61 | + super(originSession, targetSession, PropertyHelper.getInstance()); |
| 62 | + this.iPropertyHelper = propHelper; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void processPartitionRange(PartitionRange range) { |
| 67 | + // Do nothing for the test |
| 68 | + } |
| 69 | + |
| 70 | + // Make the method public for testing |
| 71 | + public void initializeSession(CqlSession originSession, CqlSession targetSession, IPropertyHelper propHelper) { |
| 72 | + try { |
| 73 | + this.originSession = new EnhancedSession(propertyHelper, originSession, true); |
| 74 | + } catch (ClusterConfigurationException e) { |
| 75 | + logger.error("Cluster configuration error detected: {}", e.getMessage()); |
| 76 | + logger.error( |
| 77 | + "Please check your Cassandra cluster for token overlap issues. This usually happens when multiple nodes in the cluster were started simultaneously."); |
| 78 | + logger.error( |
| 79 | + "You can verify this by running 'nodetool describering <keyspace>' and checking for overlapping token ranges."); |
| 80 | + logger.error( |
| 81 | + "To fix token overlap: 1) Restart nodes one at a time 2) Run nodetool cleanup on each node 3) Verify with nodetool describering"); |
| 82 | + throw e; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @BeforeEach |
| 88 | + void setUp() { |
| 89 | + // Setup common property helper mocks |
| 90 | + when(propertyHelper.getInteger(KnownProperties.PERF_RATELIMIT_ORIGIN)).thenReturn(1000); |
| 91 | + when(propertyHelper.getInteger(KnownProperties.PERF_RATELIMIT_TARGET)).thenReturn(1000); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testClusterConfigurationExceptionHandling() { |
| 96 | + // Create a subclass of AbstractJobSession that we can instrument for testing |
| 97 | + class TestableJobSession extends TestJobSession { |
| 98 | + public TestableJobSession(CqlSession originSession, CqlSession targetSession, IPropertyHelper propHelper) { |
| 99 | + super(originSession, targetSession, propHelper); |
| 100 | + } |
| 101 | + |
| 102 | + // Add our mock logger |
| 103 | + { |
| 104 | + this.logger = mockLogger; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Create an exception that would be thrown during EnhancedSession creation |
| 109 | + ClusterConfigurationException tokenOverlapException = new ClusterConfigurationException( |
| 110 | + "Token overlap detected in keyspace 'test'. This usually happens when multiple nodes were started simultaneously."); |
| 111 | + |
| 112 | + // Create a partial mock of our test session |
| 113 | + TestableJobSession jobSession = spy(new TestableJobSession(null, null, propertyHelper)); |
| 114 | + |
| 115 | + // Setup the mock to throw exception during initialization |
| 116 | + doThrow(tokenOverlapException).when(jobSession).initializeSession(any(), any(), any()); |
| 117 | + |
| 118 | + // Setup return values for propertyHelper methods that might be called |
| 119 | + when(propertyHelper.getBoolean(anyString())).thenReturn(false); |
| 120 | + when(propertyHelper.getInteger(anyString())).thenReturn(1000); |
| 121 | + when(propertyHelper.getLong(anyString())).thenReturn(1000L); |
| 122 | + when(propertyHelper.getNumber(anyString())).thenReturn(1000); |
| 123 | + when(propertyHelper.getAsString(anyString())).thenReturn(""); |
| 124 | + when(propertyHelper.getString(anyString())).thenReturn(""); |
| 125 | + when(propertyHelper.getStringList(anyString())).thenReturn(new ArrayList<>()); |
| 126 | + |
| 127 | + // When attempting to create the session with the mocked initialization |
| 128 | + ClusterConfigurationException thrown = assertThrows(ClusterConfigurationException.class, |
| 129 | + () -> jobSession.initializeSession(originSession, targetSession, propertyHelper)); |
| 130 | + |
| 131 | + // Verify the same exception is propagated |
| 132 | + assertEquals(tokenOverlapException, thrown); |
| 133 | + |
| 134 | + // Verify error logs were called |
| 135 | + verify(mockLogger).error(contains("Cluster configuration error detected")); |
| 136 | + verify(mockLogger).error(contains("Please check your Cassandra cluster for token overlap issues")); |
| 137 | + verify(mockLogger).error(contains("You can verify this by running 'nodetool describering")); |
| 138 | + verify(mockLogger).error(contains("To fix token overlap: 1) Restart nodes one at a time")); |
| 139 | + } |
| 140 | +} |
0 commit comments