/** * Wait until all Cassandra nodes agree on the schema. Sleeps 100ms between checks. * */ public void waitForSchemaAgreement() { while ( true ) { if( getClusterSession().getCluster().getMetadata().checkSchemaAgreement() ){ return; } //sleep and try it again try { Thread.sleep( 100 ); } catch ( InterruptedException e ) { //swallow } } }
@Override public Boolean call() throws Exception { return cluster.getMetadata().checkSchemaAgreement(); } }
/** * Ensures that executing a query causing a schema change with a Cluster that has schema metadata * disabled will still wait on schema agreement, but not refresh the schema. * * @jira_ticket JAVA-858 * @since 2.0.11 */ @Test(groups = "short", dataProvider = "existingKeyspaceName") public void should_not_refresh_schema_on_schema_change_response(String keyspace) throws InterruptedException { ResultSet rs = schemaDisabledSession.execute(String.format(CREATE_TABLE, keyspace)); // Should still wait on schema agreement. assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue(); assertThat(schemaDisabledCluster.getMetadata().checkSchemaAgreement()).isTrue(); // Wait up to 1 second (since refreshSchema submitted in an executor) and check that // refreshSchema never called. verify(schemaDisabledControlConnection, after(1000).never()) .refreshSchema( any(SchemaElement.class), any(String.class), any(String.class), anyListOf(String.class)); }
@Test(groups = "short") public void should_check_agreement_through_cluster_metadata() { Cluster controlCluster = register(TestUtils.buildControlCluster(cluster(), ccm())); Session controlSession = controlCluster.connect(); Row localRow = controlSession.execute("SELECT schema_version FROM system.local").one(); UUID localVersion = localRow.getUUID("schema_version"); Row peerRow = controlSession.execute("SELECT peer, schema_version FROM system.peers").one(); InetAddress peerAddress = peerRow.getInet("peer"); UUID peerVersion = peerRow.getUUID("schema_version"); // The two nodes should be in agreement at this point, but check just in case: assertThat(localVersion).isEqualTo(peerVersion); // Now check the method under test: assertThat(cluster().getMetadata().checkSchemaAgreement()).isTrue(); // Insert a fake version to simulate a disagreement: forceSchemaVersion(controlSession, peerAddress, UUIDs.random()); assertThat(cluster().getMetadata().checkSchemaAgreement()).isFalse(); forceSchemaVersion(controlSession, peerAddress, peerVersion); }
/** * Checks whether hosts that are currently up agree on the schema definition. * * This method performs a one-time check only, without any form of retry; therefore * {@link com.datastax.driver.core.Cluster.Builder#withMaxSchemaAgreementWaitSeconds(int)} * does not apply in this case. * * @return {@code true} if all hosts agree on the schema; {@code false} if * they don't agree, or if the check could not be performed * (for example, if the control connection is down). */ protected boolean checkClusterSchemaAgreement() { return this.session.getCluster().getMetadata().checkSchemaAgreement(); }
@Override public Boolean call() throws Exception { return cluster.getMetadata().checkSchemaAgreement(); } }
/** * Ensures that executing a query causing a schema change with a Cluster that has schema metadata * disabled will still wait on schema agreement, but not refresh the schema. * * @jira_ticket JAVA-858 * @since 2.0.11 */ @Test(groups = "short", dataProvider = "existingKeyspaceName") public void should_not_refresh_schema_on_schema_change_response(String keyspace) throws InterruptedException { ResultSet rs = schemaDisabledSession.execute(String.format(CREATE_TABLE, keyspace)); // Should still wait on schema agreement. assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue(); assertThat(schemaDisabledCluster.getMetadata().checkSchemaAgreement()).isTrue(); // Wait up to 1 second (since refreshSchema submitted in an executor) and check that // refreshSchema never called. verify(schemaDisabledControlConnection, after(1000).never()) .refreshSchema( any(SchemaElement.class), any(String.class), any(String.class), anyListOf(String.class)); }
@Test(groups = "short") public void should_check_agreement_through_cluster_metadata() { Cluster controlCluster = register(TestUtils.buildControlCluster(cluster(), ccm())); Session controlSession = controlCluster.connect(); Row localRow = controlSession.execute("SELECT schema_version FROM system.local").one(); UUID localVersion = localRow.getUUID("schema_version"); Row peerRow = controlSession.execute("SELECT peer, schema_version FROM system.peers").one(); InetAddress peerAddress = peerRow.getInet("peer"); UUID peerVersion = peerRow.getUUID("schema_version"); // The two nodes should be in agreement at this point, but check just in case: assertThat(localVersion).isEqualTo(peerVersion); // Now check the method under test: assertThat(cluster().getMetadata().checkSchemaAgreement()).isTrue(); // Insert a fake version to simulate a disagreement: forceSchemaVersion(controlSession, peerAddress, UUIDs.random()); assertThat(cluster().getMetadata().checkSchemaAgreement()).isFalse(); forceSchemaVersion(controlSession, peerAddress, peerVersion); }