4747import io .milvus .param .resourcegroup .*;
4848import io .milvus .param .role .*;
4949import io .milvus .response .*;
50- import io .milvus .v2 .service .collection .response .DescribeCollectionResp ;
51- import io .milvus .v2 .service .vector .request .InsertReq ;
52- import io .milvus .v2 .utils .DataUtils ;
5350import lombok .NonNull ;
5451import org .apache .commons .collections4 .CollectionUtils ;
5552import org .apache .commons .lang3 .StringUtils ;
@@ -68,22 +65,31 @@ public abstract class AbstractMilvusGrpcClient implements MilvusClient {
6865 protected static final Logger logger = LoggerFactory .getLogger (AbstractMilvusGrpcClient .class );
6966 protected LogLevel logLevel = LogLevel .Info ;
7067
71- private ConcurrentHashMap <String , DescribeCollectionResponse > cacheCollectionInfo = new ConcurrentHashMap <>();
68+ protected ConcurrentHashMap <String , DescribeCollectionResponse > cacheCollectionInfo = new ConcurrentHashMap <>();
7269
7370 protected abstract MilvusServiceGrpc .MilvusServiceBlockingStub blockingStub ();
7471
7572 protected abstract MilvusServiceGrpc .MilvusServiceFutureStub futureStub ();
7673
7774 protected abstract boolean clientIsReady ();
7875
76+ protected abstract String currentDbName ();
77+
78+ private String actualDbName (String overwriteName ) {
79+ if (StringUtils .isNotEmpty (overwriteName )) {
80+ return overwriteName ;
81+ }
82+ return currentDbName ();
83+ }
84+
7985 /**
8086 * This method is for insert/upsert requests to reduce the rpc call of describeCollection()
8187 * Always try to get the collection info from cache.
8288 * If the cache doesn't have the collection info, call describeCollection() and cache it.
8389 * If insert/upsert get server error, remove the cached collection info.
8490 */
8591 private DescribeCollectionResponse getCollectionInfo (String databaseName , String collectionName , boolean forceUpdate ) {
86- String key = combineCacheKey ( databaseName , collectionName );
92+ String key = GTsDict . CombineCollectionName ( actualDbName ( databaseName ) , collectionName );
8793 DescribeCollectionResponse info = cacheCollectionInfo .get (key );
8894 if (info == null || forceUpdate ) {
8995 String msg = String .format ("Fail to describe collection '%s'" , collectionName );
@@ -104,17 +110,6 @@ private DescribeCollectionResponse getCollectionInfo(String databaseName, String
104110 return info ;
105111 }
106112
107- private String combineCacheKey (String databaseName , String collectionName ) {
108- if (collectionName == null || StringUtils .isBlank (collectionName )) {
109- throw new ParamException ("Collection name is empty, not able to get collection info." );
110- }
111- String key = collectionName ;
112- if (StringUtils .isNotEmpty (databaseName )) {
113- key = String .format ("%s|%s" , databaseName , collectionName );
114- }
115- return key ;
116- }
117-
118113 /**
119114 * insert/upsert return an error, but is not a RateLimit error,
120115 * clean the cache so that the next insert will call describeCollection() to get the latest info.
@@ -127,7 +122,8 @@ private void cleanCacheIfFailed(Status status, String databaseName, String colle
127122 }
128123
129124 private void removeCollectionCache (String databaseName , String collectionName ) {
130- cacheCollectionInfo .remove (combineCacheKey (databaseName , collectionName ));
125+ String key = GTsDict .CombineCollectionName (actualDbName (databaseName ), collectionName );
126+ cacheCollectionInfo .remove (key );
131127 }
132128
133129 private void waitForLoadingCollection (String databaseName , String collectionName , List <String > partitionNames ,
@@ -658,7 +654,13 @@ public R<RpcStatus> dropCollection(@NonNull DropCollectionParam requestParam) {
658654
659655 Status response = blockingStub ().dropCollection (dropCollectionRequest );
660656 handleResponse (title , response );
657+
658+ // remove the collection schema cache
661659 removeCollectionCache (dbName , collectionName );
660+
661+ // remove the last write timestamp for this collection
662+ String key = GTsDict .CombineCollectionName (actualDbName (dbName ), collectionName );
663+ GTsDict .getInstance ().removeCollectionTs (key );
662664 return R .success (new RpcStatus (RpcStatus .SUCCESS_MSG ));
663665 } catch (StatusRuntimeException e ) {
664666 logError ("{} RPC failed! Exception:{}" , title , e );
@@ -1570,22 +1572,27 @@ public R<MutationResult> delete(@NonNull DeleteParam requestParam) {
15701572 }
15711573
15721574 logDebug (requestParam .toString ());
1573- String title = String .format ("DeleteRequest collectionName:%s" , requestParam .getCollectionName ());
1575+ String dbName = requestParam .getDatabaseName ();
1576+ String collectionName = requestParam .getCollectionName ();
1577+ String title = String .format ("DeleteRequest collectionName:%s" , collectionName );
15741578
15751579 try {
15761580 DeleteRequest .Builder builder = DeleteRequest .newBuilder ()
15771581 .setBase (MsgBase .newBuilder ().setMsgType (MsgType .Delete ).build ())
1578- .setCollectionName (requestParam . getCollectionName () )
1582+ .setCollectionName (collectionName )
15791583 .setPartitionName (requestParam .getPartitionName ())
15801584 .setExpr (requestParam .getExpr ());
15811585
1582- if (StringUtils .isNotEmpty (requestParam . getDatabaseName () )) {
1583- builder .setDbName (requestParam . getDatabaseName () );
1586+ if (StringUtils .isNotEmpty (dbName )) {
1587+ builder .setDbName (dbName );
15841588 }
15851589
15861590 MutationResult response = blockingStub ().delete (builder .build ());
15871591 handleResponse (title , response .getStatus ());
1588- GTsDict .getInstance ().updateCollectionTs (requestParam .getCollectionName (), response .getTimestamp ());
1592+
1593+ // update the last write timestamp for SESSION consistency
1594+ String key = GTsDict .CombineCollectionName (actualDbName (dbName ), collectionName );
1595+ GTsDict .getInstance ().updateCollectionTs (key , response .getTimestamp ());
15891596 return R .success (response );
15901597 } catch (StatusRuntimeException e ) {
15911598 logError ("{} RPC failed! Exception:{}" , title , e );
@@ -1639,10 +1646,14 @@ public R<MutationResult> insert(@NonNull InsertParam requestParam) {
16391646 return this .insert (requestParam );
16401647 }
16411648
1642- // if illegal data, server fails to process insert, else succeed
1649+ // if illegal data, server fails to process insert, , clean the schema cache
1650+ // so that the next call of dml can update the cache
16431651 cleanCacheIfFailed (response .getStatus (), dbName , collectionName );
16441652 handleResponse (title , response .getStatus ());
1645- GTsDict .getInstance ().updateCollectionTs (collectionName , response .getTimestamp ());
1653+
1654+ // update the last write timestamp for SESSION consistency
1655+ String key = GTsDict .CombineCollectionName (actualDbName (dbName ), collectionName );
1656+ GTsDict .getInstance ().updateCollectionTs (key , response .getTimestamp ());
16461657 return R .success (response );
16471658 } catch (StatusRuntimeException e ) {
16481659 logError ("{} RPC failed! Exception:{}" , title , e );
@@ -1687,11 +1698,15 @@ public ListenableFuture<R<MutationResult>> insertAsync(InsertParam requestParam)
16871698 new FutureCallback <MutationResult >() {
16881699 @ Override
16891700 public void onSuccess (MutationResult result ) {
1690- // if illegal data, server fails to process insert, else succeed
1701+ // if illegal data, server fails to process insert, clean the schema cache
1702+ // so that the next call of dml can update the cache
16911703 cleanCacheIfFailed (result .getStatus (), dbName , collectionName );
16921704 if (result .getStatus ().getErrorCode () == ErrorCode .Success ) {
16931705 logDebug ("{} successfully!" , title );
1694- GTsDict .getInstance ().updateCollectionTs (collectionName , result .getTimestamp ());
1706+
1707+ // update the last write timestamp for SESSION consistency
1708+ String key = GTsDict .CombineCollectionName (actualDbName (dbName ), collectionName );
1709+ GTsDict .getInstance ().updateCollectionTs (key , result .getTimestamp ());
16951710 } else {
16961711 logError ("{} failed:\n {}" , title , result .getStatus ().getReason ());
16971712 }
@@ -1760,10 +1775,14 @@ public R<MutationResult> upsert(UpsertParam requestParam) {
17601775 return this .upsert (requestParam );
17611776 }
17621777
1763- // if illegal data, server fails to process upsert, else succeed
1778+ // if illegal data, server fails to process upsert, clean the schema cache
1779+ // so that the next call of dml can update the cache
17641780 cleanCacheIfFailed (response .getStatus (), dbName , collectionName );
17651781 handleResponse (title , response .getStatus ());
1766- GTsDict .getInstance ().updateCollectionTs (collectionName , response .getTimestamp ());
1782+
1783+ // update the last write timestamp for SESSION consistency
1784+ String key = GTsDict .CombineCollectionName (actualDbName (dbName ), collectionName );
1785+ GTsDict .getInstance ().updateCollectionTs (key , response .getTimestamp ());
17671786 return R .success (response );
17681787 } catch (StatusRuntimeException e ) {
17691788 logError ("{} RPC failed! Exception:{}" , title , e );
@@ -1807,11 +1826,15 @@ public ListenableFuture<R<MutationResult>> upsertAsync(UpsertParam requestParam)
18071826 new FutureCallback <MutationResult >() {
18081827 @ Override
18091828 public void onSuccess (MutationResult result ) {
1810- // if illegal data, server fails to process upsert, else succeed
1829+ // if illegal data, server fails to process upsert, clean the schema cache
1830+ // so that the next call of dml can update the cache
18111831 cleanCacheIfFailed (result .getStatus (), dbName , collectionName );
18121832 if (result .getStatus ().getErrorCode () == ErrorCode .Success ) {
18131833 logDebug ("{} successfully!" , title );
1814- GTsDict .getInstance ().updateCollectionTs (collectionName , result .getTimestamp ());
1834+
1835+ // update the last write timestamp for SESSION consistency
1836+ String key = GTsDict .CombineCollectionName (actualDbName (dbName ), collectionName );
1837+ GTsDict .getInstance ().updateCollectionTs (key , result .getTimestamp ());
18151838 } else {
18161839 logError ("{} failed:\n {}" , title , result .getStatus ().getReason ());
18171840 }
0 commit comments