diff --git a/src/main/java/org/commonjava/indy/service/tracking/data/cassandra/CassandraTrackingQuery.java b/src/main/java/org/commonjava/indy/service/tracking/data/cassandra/CassandraTrackingQuery.java index f17affe..72657bb 100644 --- a/src/main/java/org/commonjava/indy/service/tracking/data/cassandra/CassandraTrackingQuery.java +++ b/src/main/java/org/commonjava/indy/service/tracking/data/cassandra/CassandraTrackingQuery.java @@ -16,6 +16,7 @@ package org.commonjava.indy.service.tracking.data.cassandra; import com.datastax.driver.core.*; +import com.datastax.driver.core.exceptions.NoHostAvailableException; import com.datastax.driver.mapping.Mapper; import com.datastax.driver.mapping.MappingManager; import org.commonjava.indy.service.tracking.exception.ContentException; @@ -130,6 +131,37 @@ public void init() logger.info( "-- Cassandra Folo Records Keyspace and Tables created" ); } + private ResultSet executeSession ( BoundStatement bind ) + { + boolean exception = false; + ResultSet trackingRecord = null; + try + { + if ( session == null ) + { + client.close(); + client.init(); + this.init(); + } + trackingRecord = session.execute( bind ); + } + catch ( NoHostAvailableException e ) + { + exception = true; + logger.error( "Cannot connect to host, reconnect once more with new session.", e ); + } + finally + { + if ( exception ) + { + client.close(); + client.init(); + this.init(); + trackingRecord = session.execute( bind ); + } + } + return trackingRecord; + } public boolean recordArtifact( TrackedContentEntry entry ) throws ContentException, IndyWorkflowException { @@ -139,7 +171,7 @@ public boolean recordArtifact( TrackedContentEntry entry ) throws ContentExcepti String effect = entry.getEffect().toString(); BoundStatement bind = getTrackingRecord.bind( buildId, storeKey, path, effect ); - ResultSet trackingRecord = session.execute( bind ); + ResultSet trackingRecord = executeSession( bind ); Row one = trackingRecord.one(); if ( one != null ) @@ -161,7 +193,7 @@ public void delete( TrackingKey key ) { logger.info( "Delete tracking records, tracking_id: {}", key.getId() ); BoundStatement bind = deleteTrackingRecordsByTrackingKey.bind( key.getId() ); - session.execute( bind ); + executeSession( bind ); } public void replaceTrackingRecord( TrackedContent record ) @@ -172,7 +204,7 @@ public void replaceTrackingRecord( TrackedContent record ) public boolean hasRecord( TrackingKey key ) { BoundStatement bind = isTrackingRecordExist.bind( key ); - ResultSet result = session.execute( bind ); + ResultSet result = executeSession( bind ); Row row = result.one(); boolean exists = false; if ( row != null ) @@ -303,7 +335,7 @@ private TrackedContent transformDtxTrackingRecordToTrackingContent( TrackingKey private List getLegacyDtxTrackingRecordsFromDb( TrackingKey trackingKey ) { BoundStatement bind = getLegacyTrackingRecordsByTrackingKey.bind( trackingKey.getId() ); - ResultSet execute = session.execute( bind ); + ResultSet execute = executeSession( bind ); List rows = execute.all(); return fetchRecordsFromRows( rows ); } @@ -311,7 +343,7 @@ private List getLegacyDtxTrackingRecordsFromDb( TrackingKey t private List getDtxTrackingRecordsFromDb( TrackingKey trackingKey ) { BoundStatement bind = getTrackingRecordsByTrackingKey.bind( trackingKey.getId() ); - ResultSet execute = session.execute( bind ); + ResultSet execute = executeSession( bind ); List rows = execute.all(); return fetchRecordsFromRows( rows ); } @@ -385,7 +417,7 @@ private Set getTrackingKeys() private Set getTrackingKeys( BoundStatement statement ) { - ResultSet resultSet = session.execute( statement ); + ResultSet resultSet = executeSession( statement ); List all = resultSet.all(); Iterator iterator = all.iterator();