MilvusServiceClientTest.java 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package io.milvus.client;
  20. import com.google.common.util.concurrent.ListenableFuture;
  21. import com.google.protobuf.ByteString;
  22. import io.milvus.common.clientenum.ConsistencyLevelEnum;
  23. import io.milvus.exception.IllegalResponseException;
  24. import io.milvus.exception.ParamException;
  25. import io.milvus.grpc.*;
  26. import io.milvus.param.*;
  27. import io.milvus.param.alias.AlterAliasParam;
  28. import io.milvus.param.alias.CreateAliasParam;
  29. import io.milvus.param.alias.DropAliasParam;
  30. import io.milvus.param.collection.*;
  31. import io.milvus.param.control.*;
  32. import io.milvus.param.credential.*;
  33. import io.milvus.param.dml.*;
  34. import io.milvus.param.index.*;
  35. import io.milvus.param.partition.*;
  36. import io.milvus.response.*;
  37. import io.milvus.server.MockMilvusServer;
  38. import io.milvus.server.MockMilvusServerImpl;
  39. import org.junit.jupiter.api.Test;
  40. import java.lang.reflect.InvocationTargetException;
  41. import java.lang.reflect.Method;
  42. import java.nio.ByteBuffer;
  43. import java.util.*;
  44. import java.util.concurrent.ExecutionException;
  45. import java.util.concurrent.TimeUnit;
  46. import static org.junit.jupiter.api.Assertions.*;
  47. class MilvusServiceClientTest {
  48. private final int testPort = 53019;
  49. private MockMilvusServerImpl mockServerImpl;
  50. private MockMilvusServer startServer() {
  51. mockServerImpl = new MockMilvusServerImpl();
  52. MockMilvusServer mockServer = new MockMilvusServer(testPort, mockServerImpl);
  53. mockServer.start();
  54. return mockServer;
  55. }
  56. private MilvusServiceClient startClient() {
  57. String testHost = "localhost";
  58. ConnectParam connectParam = ConnectParam.newBuilder()
  59. .withHost(testHost)
  60. .withPort(testPort)
  61. .build();
  62. return new MilvusServiceClient(connectParam);
  63. }
  64. @SuppressWarnings("unchecked")
  65. private <T, P> void invokeFunc(Method testFunc, MilvusServiceClient client, T param, int ret, boolean equalRet) {
  66. try {
  67. R<P> resp = (R<P>) testFunc.invoke(client, param);
  68. if (equalRet) {
  69. assertEquals(ret, resp.getStatus());
  70. } else {
  71. assertNotEquals(ret, resp.getStatus());
  72. }
  73. } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
  74. e.printStackTrace();
  75. System.out.println(e.getMessage());
  76. fail();
  77. }
  78. }
  79. private <T, P> void testFuncByName(String funcName, T param) {
  80. // start mock server
  81. MockMilvusServer server = startServer();
  82. MilvusServiceClient client = startClient();
  83. try {
  84. Class<?> clientClass = MilvusServiceClient.class;
  85. Method testFunc = clientClass.getMethod(funcName, param.getClass());
  86. // test return ok with correct input
  87. invokeFunc(testFunc, client, param, R.Status.Success.getCode(), true);
  88. // stop mock server
  89. server.stop();
  90. // test return error without server
  91. invokeFunc(testFunc, client, param, R.Status.Success.getCode(), false);
  92. // test return error when client channel is shutdown
  93. client.close();
  94. invokeFunc(testFunc, client, param, R.Status.ClientNotConnected.getCode(), true);
  95. } catch (NoSuchMethodException | SecurityException e) {
  96. e.printStackTrace();
  97. System.out.println(e.getMessage());
  98. fail();
  99. } finally {
  100. server.stop();
  101. client.close();
  102. }
  103. }
  104. @SuppressWarnings("unchecked")
  105. private <T, P> void testAsyncFuncByName(String funcName, T param) {
  106. // start mock server
  107. MockMilvusServer server = startServer();
  108. MilvusServiceClient client = startClient();
  109. try {
  110. Class<?> clientClass = MilvusServiceClient.class;
  111. Method testFunc = clientClass.getMethod(funcName, param.getClass());
  112. // test return ok with correct input
  113. try {
  114. ListenableFuture<R<P>> respFuture = (ListenableFuture<R<P>>) testFunc.invoke(client, param);
  115. R<P> response = respFuture.get();
  116. assertEquals(R.Status.Success.getCode(), response.getStatus());
  117. } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException |
  118. InterruptedException | ExecutionException e) {
  119. e.printStackTrace();
  120. System.out.println(e.getMessage());
  121. fail();
  122. }
  123. // stop mock server
  124. server.stop();
  125. // test return error without server
  126. assertThrows(ExecutionException.class, () -> {
  127. ListenableFuture<R<P>> respFuture = (ListenableFuture<R<P>>) testFunc.invoke(client, param);
  128. R<P> response = respFuture.get();
  129. assertNotEquals(R.Status.Success.getCode(), response.getStatus());
  130. });
  131. // test return error when client channel is shutdown
  132. client.close();
  133. try {
  134. ListenableFuture<R<P>> respFuture = (ListenableFuture<R<P>>) testFunc.invoke(client, param);
  135. R<P> response = respFuture.get();
  136. assertEquals(R.Status.ClientNotConnected.getCode(), response.getStatus());
  137. } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException |
  138. InterruptedException | ExecutionException e) {
  139. e.printStackTrace();
  140. System.out.println(e.getMessage());
  141. fail();
  142. }
  143. } catch (NoSuchMethodException | SecurityException e) {
  144. e.printStackTrace();
  145. System.out.println(e.getMessage());
  146. fail();
  147. } finally {
  148. server.stop();
  149. client.close();
  150. }
  151. }
  152. @Test
  153. void r() {
  154. String msg = "error";
  155. R<RpcStatus> r = R.failed(ErrorCode.UnexpectedError, msg);
  156. Exception e = r.getException();
  157. assertEquals(0, msg.compareTo(e.getMessage()));
  158. System.out.println(r.toString());
  159. r = R.success();
  160. assertEquals(R.Status.Success.getCode(), r.getStatus());
  161. System.out.println(r.toString());
  162. }
  163. @Test
  164. void connectParam() {
  165. System.out.println(System.getProperty("os.name"));
  166. System.out.println(System.getProperty("os.arch"));
  167. String host = "dummyHost";
  168. int port = 100;
  169. long connectTimeoutMs = 1;
  170. long keepAliveTimeMs = 2;
  171. long keepAliveTimeoutMs = 3;
  172. long idleTimeoutMs = 5;
  173. ConnectParam connectParam = ConnectParam.newBuilder()
  174. .withHost(host)
  175. .withPort(port)
  176. .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
  177. .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
  178. .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
  179. .keepAliveWithoutCalls(true)
  180. .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
  181. .build();
  182. System.out.println(connectParam.toString());
  183. assertEquals(0, host.compareTo(connectParam.getHost()));
  184. assertEquals(port, connectParam.getPort());
  185. assertEquals(connectTimeoutMs, connectParam.getConnectTimeoutMs());
  186. assertEquals(keepAliveTimeMs, connectParam.getKeepAliveTimeMs());
  187. assertEquals(keepAliveTimeoutMs, connectParam.getKeepAliveTimeoutMs());
  188. assertTrue(connectParam.isKeepAliveWithoutCalls());
  189. assertEquals(idleTimeoutMs, connectParam.getIdleTimeoutMs());
  190. assertThrows(ParamException.class, () ->
  191. ConnectParam.newBuilder()
  192. .withHost(host)
  193. .withPort(0xFFFF + 1)
  194. .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
  195. .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
  196. .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
  197. .keepAliveWithoutCalls(true)
  198. .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
  199. .build()
  200. );
  201. assertThrows(ParamException.class, () ->
  202. ConnectParam.newBuilder()
  203. .withHost(host)
  204. .withPort(port)
  205. .withConnectTimeout(-1, TimeUnit.MILLISECONDS)
  206. .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
  207. .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
  208. .keepAliveWithoutCalls(true)
  209. .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
  210. .build()
  211. );
  212. assertThrows(ParamException.class, () ->
  213. ConnectParam.newBuilder()
  214. .withHost(host)
  215. .withPort(port)
  216. .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
  217. .withKeepAliveTime(-1, TimeUnit.MILLISECONDS)
  218. .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
  219. .keepAliveWithoutCalls(true)
  220. .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
  221. .build()
  222. );
  223. assertThrows(ParamException.class, () ->
  224. ConnectParam.newBuilder()
  225. .withHost(host)
  226. .withPort(port)
  227. .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
  228. .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
  229. .withKeepAliveTimeout(-1, TimeUnit.NANOSECONDS)
  230. .keepAliveWithoutCalls(true)
  231. .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
  232. .build()
  233. );
  234. assertThrows(ParamException.class, () ->
  235. ConnectParam.newBuilder()
  236. .withHost(host)
  237. .withPort(port)
  238. .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
  239. .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
  240. .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
  241. .keepAliveWithoutCalls(true)
  242. .withIdleTimeout(-1, TimeUnit.MILLISECONDS)
  243. .build()
  244. );
  245. }
  246. @Test
  247. void createCollectionParam() {
  248. // test throw exception with illegal input for FieldType
  249. assertThrows(ParamException.class, () ->
  250. FieldType.newBuilder()
  251. .withName("")
  252. .withDataType(DataType.Int64)
  253. .build()
  254. );
  255. assertThrows(ParamException.class, () ->
  256. FieldType.newBuilder()
  257. .withName("userID")
  258. .build()
  259. );
  260. assertThrows(ParamException.class, () ->
  261. FieldType.newBuilder()
  262. .withName("userID")
  263. .withDataType(DataType.FloatVector)
  264. .build()
  265. );
  266. assertThrows(ParamException.class, () ->
  267. FieldType.newBuilder()
  268. .withName("userID")
  269. .withDataType(DataType.Int64)
  270. .withPrimaryKey(true)
  271. .withPartitionKey(true)
  272. .build()
  273. );
  274. assertThrows(ParamException.class, () ->
  275. FieldType.newBuilder()
  276. .withName("userID")
  277. .withDataType(DataType.FloatVector)
  278. .withPartitionKey(true)
  279. .build()
  280. );
  281. assertDoesNotThrow(() ->
  282. FieldType.newBuilder()
  283. .withName("partitionKey")
  284. .withDataType(DataType.Int64)
  285. .withPartitionKey(true)
  286. .build()
  287. );
  288. assertDoesNotThrow(() ->
  289. FieldType.newBuilder()
  290. .withName("partitionKey")
  291. .withDataType(DataType.VarChar)
  292. .withMaxLength(120)
  293. .withPartitionKey(true)
  294. .build()
  295. );
  296. Map<String, String> params = new HashMap<>();
  297. params.put("1", "1");
  298. assertThrows(ParamException.class, () ->
  299. FieldType.newBuilder()
  300. .withName("vec")
  301. .withDescription("desc")
  302. .withDataType(DataType.FloatVector)
  303. .withTypeParams(params)
  304. .addTypeParam("2", "2")
  305. .withDimension(-1)
  306. .build()
  307. );
  308. // test throw exception with illegal input for CreateCollectionParam
  309. assertThrows(ParamException.class, () ->
  310. CreateCollectionParam
  311. .newBuilder()
  312. .withCollectionName("collection1")
  313. .withShardsNum(2)
  314. .build()
  315. );
  316. FieldType fieldType1 = FieldType.newBuilder()
  317. .withName("userID")
  318. .withDescription("userId")
  319. .withDataType(DataType.Int64)
  320. .withAutoID(true)
  321. .withPrimaryKey(true)
  322. .build();
  323. assertThrows(ParamException.class, () ->
  324. CreateCollectionParam
  325. .newBuilder()
  326. .withCollectionName("")
  327. .withShardsNum(2)
  328. .addFieldType(fieldType1)
  329. .build()
  330. );
  331. assertThrows(ParamException.class, () ->
  332. CreateCollectionParam
  333. .newBuilder()
  334. .withCollectionName("collection1")
  335. .withShardsNum(-1)
  336. .addFieldType(fieldType1)
  337. .build()
  338. );
  339. List<FieldType> fields = Collections.singletonList(null);
  340. assertThrows(ParamException.class, () ->
  341. CreateCollectionParam
  342. .newBuilder()
  343. .withCollectionName("collection1")
  344. .withShardsNum(2)
  345. .withFieldTypes(fields)
  346. .build()
  347. );
  348. assertThrows(ParamException.class, () ->
  349. CreateCollectionParam
  350. .newBuilder()
  351. .withCollectionName("collection1")
  352. .withShardsNum(0)
  353. .withPartitionsNum(10)
  354. .addFieldType(fieldType1)
  355. .build()
  356. );
  357. FieldType fieldType2 = FieldType.newBuilder()
  358. .withName("partitionKey")
  359. .withDataType(DataType.Int64)
  360. .withPartitionKey(true)
  361. .build();
  362. assertDoesNotThrow(() ->
  363. CreateCollectionParam
  364. .newBuilder()
  365. .withCollectionName("collection1")
  366. .addFieldType(fieldType1)
  367. .addFieldType(fieldType2)
  368. .build()
  369. );
  370. assertDoesNotThrow(() ->
  371. CreateCollectionParam
  372. .newBuilder()
  373. .withCollectionName("collection1")
  374. .withPartitionsNum(100)
  375. .addFieldType(fieldType1)
  376. .addFieldType(fieldType2)
  377. .build()
  378. );
  379. }
  380. @Test
  381. void createCollection() {
  382. FieldType fieldType1 = FieldType.newBuilder()
  383. .withName("userID")
  384. .withDescription("userId")
  385. .withDataType(DataType.Int64)
  386. .withAutoID(true)
  387. .withPrimaryKey(true)
  388. .build();
  389. CreateCollectionParam param = CreateCollectionParam
  390. .newBuilder()
  391. .withCollectionName("collection1")
  392. .withDescription("desc")
  393. .withShardsNum(2)
  394. .addFieldType(fieldType1)
  395. .build();
  396. testFuncByName("createCollection", param);
  397. }
  398. @Test
  399. void describeCollectionParam() {
  400. // test throw exception with illegal input
  401. assertThrows(ParamException.class, () ->
  402. DescribeCollectionParam.newBuilder()
  403. .withCollectionName("")
  404. .build()
  405. );
  406. }
  407. @Test
  408. void describeCollection() {
  409. DescribeCollectionParam param = DescribeCollectionParam.newBuilder()
  410. .withCollectionName("collection1")
  411. .build();
  412. testFuncByName("describeCollection", param);
  413. }
  414. @Test
  415. void dropCollectionParam() {
  416. // test throw exception with illegal input
  417. assertThrows(ParamException.class, () ->
  418. DropCollectionParam.newBuilder()
  419. .withCollectionName("")
  420. .build()
  421. );
  422. }
  423. @Test
  424. void dropCollection() {
  425. DropCollectionParam param = DropCollectionParam.newBuilder()
  426. .withCollectionName("collection1")
  427. .build();
  428. testFuncByName("dropCollection", param);
  429. }
  430. @Test
  431. void getCollectionStatisticsParam() {
  432. // test throw exception with illegal input
  433. assertThrows(ParamException.class, () ->
  434. GetCollectionStatisticsParam.newBuilder()
  435. .withCollectionName("")
  436. .build()
  437. );
  438. }
  439. @Test
  440. void getCollectionStatistics() {
  441. // start mock server
  442. MockMilvusServer server = startServer();
  443. MilvusServiceClient client = startClient();
  444. try {
  445. final String collectionName = "collection1";
  446. GetCollectionStatisticsParam param = GetCollectionStatisticsParam.newBuilder()
  447. .withCollectionName(collectionName)
  448. .withFlush(Boolean.TRUE)
  449. .build();
  450. // test return ok with correct input
  451. final long segmentID = 2021L;
  452. mockServerImpl.setFlushResponse(FlushResponse.newBuilder()
  453. .putCollSegIDs(collectionName, LongArray.newBuilder().addData(segmentID).build())
  454. .build());
  455. mockServerImpl.setGetFlushStateResponse(GetFlushStateResponse.newBuilder()
  456. .setFlushed(false)
  457. .build());
  458. new Thread(() -> {
  459. try {
  460. TimeUnit.SECONDS.sleep(1);
  461. } catch (InterruptedException e) {
  462. System.out.println(e.toString());
  463. }
  464. mockServerImpl.setGetFlushStateResponse(GetFlushStateResponse.newBuilder()
  465. .setFlushed(true)
  466. .build());
  467. }, "RefreshFlushState").start();
  468. R<GetCollectionStatisticsResponse> resp = client.getCollectionStatistics(param);
  469. assertEquals(R.Status.Success.getCode(), resp.getStatus());
  470. // stop mock server
  471. server.stop();
  472. // test return error without server
  473. resp = client.getCollectionStatistics(param);
  474. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  475. // test return error when client channel is shutdown
  476. client.close();
  477. resp = client.getCollectionStatistics(param);
  478. assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
  479. } catch (Exception e) {
  480. e.printStackTrace();
  481. System.out.println(e.getMessage());
  482. fail();
  483. } finally {
  484. server.stop();
  485. client.close();
  486. }
  487. }
  488. @Test
  489. void hasCollectionParam() {
  490. // test throw exception with illegal input
  491. assertThrows(ParamException.class, () ->
  492. HasCollectionParam.newBuilder()
  493. .withCollectionName("")
  494. .build()
  495. );
  496. }
  497. @Test
  498. void hasCollection() {
  499. HasCollectionParam param = HasCollectionParam.newBuilder()
  500. .withCollectionName("collection1")
  501. .build();
  502. testFuncByName("hasCollection", param);
  503. }
  504. @Test
  505. void loadCollectionParam() {
  506. // test throw exception with illegal input
  507. assertThrows(ParamException.class, () ->
  508. LoadCollectionParam.newBuilder()
  509. .withCollectionName("")
  510. .build()
  511. );
  512. assertThrows(ParamException.class, () ->
  513. LoadCollectionParam.newBuilder()
  514. .withCollectionName("collection1")
  515. .withSyncLoad(Boolean.TRUE)
  516. .withSyncLoadWaitingInterval(0L)
  517. .build()
  518. );
  519. assertThrows(ParamException.class, () ->
  520. LoadCollectionParam.newBuilder()
  521. .withCollectionName("collection1")
  522. .withSyncLoad(Boolean.TRUE)
  523. .withSyncLoadWaitingInterval(-1L)
  524. .build()
  525. );
  526. assertThrows(ParamException.class, () ->
  527. LoadCollectionParam.newBuilder()
  528. .withCollectionName("collection1")
  529. .withSyncLoad(Boolean.TRUE)
  530. .withSyncLoadWaitingInterval(Constant.MAX_WAITING_LOADING_INTERVAL + 1)
  531. .build()
  532. );
  533. assertThrows(ParamException.class, () ->
  534. LoadCollectionParam.newBuilder()
  535. .withCollectionName("collection1")
  536. .withSyncLoad(Boolean.TRUE)
  537. .withSyncLoadWaitingTimeout(0L)
  538. .build()
  539. );
  540. assertThrows(ParamException.class, () ->
  541. LoadCollectionParam.newBuilder()
  542. .withCollectionName("collection1")
  543. .withSyncLoad(Boolean.TRUE)
  544. .withSyncLoadWaitingTimeout(-1L)
  545. .build()
  546. );
  547. assertThrows(ParamException.class, () ->
  548. LoadCollectionParam.newBuilder()
  549. .withCollectionName("collection1")
  550. .withSyncLoad(Boolean.TRUE)
  551. .withSyncLoadWaitingTimeout(Constant.MAX_WAITING_LOADING_TIMEOUT + 1)
  552. .build()
  553. );
  554. }
  555. @Test
  556. void loadCollection() {
  557. // start mock server
  558. MockMilvusServer server = startServer();
  559. MilvusServiceClient client = startClient();
  560. String collectionName = "collection1";
  561. LoadCollectionParam param = LoadCollectionParam.newBuilder()
  562. .withCollectionName(collectionName)
  563. .withSyncLoad(Boolean.FALSE)
  564. .build();
  565. // test return ok with correct input
  566. R<RpcStatus> resp = client.loadCollection(param);
  567. assertEquals(R.Status.Success.getCode(), resp.getStatus());
  568. // test return ok for sync mode loading
  569. mockServerImpl.setShowCollectionsResponse(ShowCollectionsResponse.newBuilder()
  570. .addCollectionNames(collectionName)
  571. .addInMemoryPercentages(0)
  572. .build());
  573. new Thread(() -> {
  574. try {
  575. for (int i = 0; i <= 10; ++i) {
  576. TimeUnit.MILLISECONDS.sleep(100);
  577. mockServerImpl.setShowCollectionsResponse(ShowCollectionsResponse.newBuilder()
  578. .addCollectionNames(collectionName)
  579. .addInMemoryPercentages(i * 10)
  580. .build());
  581. }
  582. } catch (InterruptedException e) {
  583. mockServerImpl.setShowCollectionsResponse(ShowCollectionsResponse.newBuilder()
  584. .addCollectionNames(collectionName)
  585. .addInMemoryPercentages(100)
  586. .build());
  587. }
  588. }, "RefreshMemState").start();
  589. param = LoadCollectionParam.newBuilder()
  590. .withCollectionName(collectionName)
  591. .withSyncLoad(Boolean.TRUE)
  592. .withSyncLoadWaitingInterval(100L)
  593. .build();
  594. resp = client.loadCollection(param);
  595. assertEquals(R.Status.Success.getCode(), resp.getStatus());
  596. // stop mock server
  597. server.stop();
  598. // test return error without server
  599. resp = client.loadCollection(param);
  600. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  601. // test return error when client channel is shutdown
  602. client.close();
  603. resp = client.loadCollection(param);
  604. assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
  605. }
  606. @Test
  607. void releaseCollectionParam() {
  608. // test throw exception with illegal input
  609. assertThrows(ParamException.class, () ->
  610. ReleaseCollectionParam.newBuilder()
  611. .withCollectionName("")
  612. .build()
  613. );
  614. }
  615. @Test
  616. void releaseCollection() {
  617. ReleaseCollectionParam param = ReleaseCollectionParam.newBuilder()
  618. .withCollectionName("collection1")
  619. .build();
  620. testFuncByName("releaseCollection", param);
  621. }
  622. @Test
  623. void showCollectionsParam() {
  624. // test throw exception with illegal input
  625. List<String> names = new ArrayList<>();
  626. names.add(null);
  627. assertThrows(NullPointerException.class, () ->
  628. ShowCollectionsParam.newBuilder()
  629. .withCollectionNames(names)
  630. .build()
  631. );
  632. assertThrows(ParamException.class, () ->
  633. ShowCollectionsParam.newBuilder()
  634. .addCollectionName("")
  635. .build()
  636. );
  637. // verify internal param
  638. ShowCollectionsParam param = ShowCollectionsParam.newBuilder()
  639. .build();
  640. assertEquals(ShowType.All, param.getShowType());
  641. param = ShowCollectionsParam.newBuilder()
  642. .addCollectionName("collection1")
  643. .build();
  644. assertEquals(ShowType.InMemory, param.getShowType());
  645. }
  646. @Test
  647. void showCollections() {
  648. ShowCollectionsParam param = ShowCollectionsParam.newBuilder()
  649. .addCollectionName("collection1")
  650. .addCollectionName("collection2")
  651. .build();
  652. testFuncByName("showCollections", param);
  653. }
  654. @Test
  655. void alterCollectionParam() {
  656. AlterCollectionParam param = AlterCollectionParam.newBuilder()
  657. .withCollectionName("collection1")
  658. .build();
  659. assertEquals("collection1", param.getCollectionName());
  660. assertTrue(param.getProperties().isEmpty());
  661. param = AlterCollectionParam.newBuilder()
  662. .withCollectionName("collection1")
  663. .withTTL(100)
  664. .build();
  665. assertTrue(param.getProperties().containsKey(Constant.TTL_SECONDS));
  666. assertEquals("100", param.getProperties().get(Constant.TTL_SECONDS));
  667. assertThrows(ParamException.class, () ->
  668. AlterCollectionParam.newBuilder()
  669. .withCollectionName("")
  670. .build()
  671. );
  672. assertThrows(ParamException.class, () ->
  673. AlterCollectionParam.newBuilder()
  674. .withCollectionName("collection1")
  675. .withTTL(-5)
  676. .build()
  677. );
  678. }
  679. @Test
  680. void alterCollection() {
  681. AlterCollectionParam param = AlterCollectionParam.newBuilder()
  682. .withCollectionName("collection1")
  683. .withTTL(100)
  684. .build();
  685. testFuncByName("alterCollection", param);
  686. }
  687. @Test
  688. void flushParam() {
  689. // test throw exception with illegal input
  690. assertThrows(ParamException.class, () -> FlushParam.newBuilder()
  691. .build()
  692. );
  693. assertThrows(ParamException.class, () -> FlushParam.newBuilder()
  694. .addCollectionName("")
  695. .build()
  696. );
  697. assertThrows(ParamException.class, () -> FlushParam.newBuilder()
  698. .addCollectionName("collection1")
  699. .withSyncFlush(Boolean.TRUE)
  700. .withSyncFlushWaitingInterval(0L)
  701. .build()
  702. );
  703. assertThrows(ParamException.class, () -> FlushParam.newBuilder()
  704. .addCollectionName("collection1")
  705. .withSyncFlush(Boolean.TRUE)
  706. .withSyncFlushWaitingInterval(-1L)
  707. .build()
  708. );
  709. assertThrows(ParamException.class, () -> FlushParam.newBuilder()
  710. .withCollectionNames(Collections.singletonList("collection1"))
  711. .withSyncFlush(Boolean.TRUE)
  712. .withSyncFlushWaitingInterval(Constant.MAX_WAITING_FLUSHING_INTERVAL + 1)
  713. .build()
  714. );
  715. assertThrows(ParamException.class, () -> FlushParam.newBuilder()
  716. .addCollectionName("collection1")
  717. .withSyncFlush(Boolean.TRUE)
  718. .withSyncFlushWaitingTimeout(0L)
  719. .build()
  720. );
  721. assertThrows(ParamException.class, () -> FlushParam.newBuilder()
  722. .addCollectionName("collection1")
  723. .withSyncFlush(Boolean.TRUE)
  724. .withSyncFlushWaitingTimeout(-1L)
  725. .build()
  726. );
  727. assertThrows(ParamException.class, () -> FlushParam.newBuilder()
  728. .addCollectionName("collection1")
  729. .withSyncFlush(Boolean.TRUE)
  730. .withSyncFlushWaitingTimeout(Constant.MAX_WAITING_FLUSHING_TIMEOUT + 1)
  731. .build()
  732. );
  733. }
  734. @Test
  735. void flush() {
  736. FlushParam param = FlushParam.newBuilder()
  737. .addCollectionName("collection1")
  738. .withSyncFlush(Boolean.TRUE)
  739. .withSyncFlushWaitingTimeout(1L)
  740. .build();
  741. testFuncByName("flush", param);
  742. }
  743. @Test
  744. void createPartitionParam() {
  745. // test throw exception with illegal input
  746. assertThrows(ParamException.class, () -> CreatePartitionParam.newBuilder()
  747. .withCollectionName("")
  748. .withPartitionName("partition1")
  749. .build()
  750. );
  751. assertThrows(ParamException.class, () -> CreatePartitionParam.newBuilder()
  752. .withCollectionName("collection1")
  753. .withPartitionName("")
  754. .build()
  755. );
  756. }
  757. @Test
  758. void createPartition() {
  759. CreatePartitionParam param = CreatePartitionParam.newBuilder()
  760. .withCollectionName("collection1")
  761. .withPartitionName("partition1")
  762. .build();
  763. testFuncByName("createPartition", param);
  764. }
  765. @Test
  766. void dropPartitionParam() {
  767. // test throw exception with illegal input
  768. assertThrows(ParamException.class, () -> DropPartitionParam.newBuilder()
  769. .withCollectionName("")
  770. .withPartitionName("partition1")
  771. .build()
  772. );
  773. assertThrows(ParamException.class, () -> DropPartitionParam.newBuilder()
  774. .withCollectionName("collection1")
  775. .withPartitionName("")
  776. .build()
  777. );
  778. }
  779. @Test
  780. void dropPartition() {
  781. DropPartitionParam param = DropPartitionParam.newBuilder()
  782. .withCollectionName("collection1")
  783. .withPartitionName("partition1")
  784. .build();
  785. testFuncByName("dropPartition", param);
  786. }
  787. @Test
  788. void hasPartitionParam() {
  789. // test throw exception with illegal input
  790. assertThrows(ParamException.class, () -> HasPartitionParam.newBuilder()
  791. .withCollectionName("")
  792. .withPartitionName("partition1")
  793. .build()
  794. );
  795. assertThrows(ParamException.class, () -> HasPartitionParam.newBuilder()
  796. .withCollectionName("collection1")
  797. .withPartitionName("")
  798. .build()
  799. );
  800. }
  801. @Test
  802. void hasPartition() {
  803. HasPartitionParam param = HasPartitionParam.newBuilder()
  804. .withCollectionName("collection1")
  805. .withPartitionName("partition1")
  806. .build();
  807. testFuncByName("hasPartition", param);
  808. }
  809. @Test
  810. void loadPartitionsParam() {
  811. // test throw exception with illegal input
  812. assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
  813. .withCollectionName("")
  814. .addPartitionName("partition1")
  815. .build()
  816. );
  817. assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
  818. .withCollectionName("collection1")
  819. .addPartitionName("")
  820. .build()
  821. );
  822. assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
  823. .withCollectionName("collection1")
  824. .build()
  825. );
  826. List<String> names = new ArrayList<>();
  827. names.add(null);
  828. assertThrows(NullPointerException.class, () -> LoadPartitionsParam.newBuilder()
  829. .withCollectionName("collection1")
  830. .withPartitionNames(names)
  831. .build()
  832. );
  833. assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
  834. .withCollectionName("collection1")
  835. .addPartitionName("partition1")
  836. .withSyncLoad(Boolean.TRUE)
  837. .withSyncLoadWaitingInterval(0L)
  838. .build()
  839. );
  840. assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
  841. .withCollectionName("collection1")
  842. .addPartitionName("partition1")
  843. .withSyncLoad(Boolean.TRUE)
  844. .withSyncLoadWaitingInterval(-1L)
  845. .build()
  846. );
  847. assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
  848. .withCollectionName("collection1")
  849. .addPartitionName("partition1")
  850. .withSyncLoad(Boolean.TRUE)
  851. .withSyncLoadWaitingInterval(Constant.MAX_WAITING_LOADING_INTERVAL + 1)
  852. .build()
  853. );
  854. assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
  855. .withCollectionName("collection1")
  856. .addPartitionName("partition1")
  857. .withSyncLoad(Boolean.TRUE)
  858. .withSyncLoadWaitingTimeout(0L)
  859. .build()
  860. );
  861. assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
  862. .withCollectionName("collection1")
  863. .addPartitionName("partition1")
  864. .withSyncLoad(Boolean.TRUE)
  865. .withSyncLoadWaitingTimeout(-1L)
  866. .build()
  867. );
  868. assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
  869. .withCollectionName("collection1")
  870. .addPartitionName("partition1")
  871. .withSyncLoad(Boolean.TRUE)
  872. .withSyncLoadWaitingTimeout(Constant.MAX_WAITING_LOADING_TIMEOUT + 1)
  873. .build()
  874. );
  875. }
  876. @Test
  877. void loadPartitions() {
  878. // start mock server
  879. MockMilvusServer server = startServer();
  880. MilvusServiceClient client = startClient();
  881. String collectionName = "collection1";
  882. String partitionName = "partition1";
  883. LoadPartitionsParam param = LoadPartitionsParam.newBuilder()
  884. .withCollectionName(collectionName)
  885. .addPartitionName(partitionName)
  886. .withSyncLoad(Boolean.FALSE)
  887. .build();
  888. // test return ok with correct input
  889. R<RpcStatus> resp = client.loadPartitions(param);
  890. assertEquals(R.Status.Success.getCode(), resp.getStatus());
  891. // test return ok for sync mode loading
  892. mockServerImpl.setShowPartitionsResponse(ShowPartitionsResponse.newBuilder()
  893. .addPartitionNames(partitionName)
  894. .addInMemoryPercentages(0)
  895. .build());
  896. new Thread(() -> {
  897. try {
  898. for (int i = 0; i <= 10; ++i) {
  899. TimeUnit.MILLISECONDS.sleep(100);
  900. mockServerImpl.setShowPartitionsResponse(ShowPartitionsResponse.newBuilder()
  901. .addPartitionNames(partitionName)
  902. .addInMemoryPercentages(i * 10)
  903. .build());
  904. }
  905. } catch (InterruptedException e) {
  906. mockServerImpl.setShowPartitionsResponse(ShowPartitionsResponse.newBuilder()
  907. .addPartitionNames(partitionName)
  908. .addInMemoryPercentages(100)
  909. .build());
  910. }
  911. }, "RefreshMemState").start();
  912. param = LoadPartitionsParam.newBuilder()
  913. .withCollectionName(collectionName)
  914. .addPartitionName(partitionName)
  915. .withSyncLoad(Boolean.TRUE)
  916. .withSyncLoadWaitingInterval(100L)
  917. .build();
  918. resp = client.loadPartitions(param);
  919. assertEquals(R.Status.Success.getCode(), resp.getStatus());
  920. // stop mock server
  921. server.stop();
  922. // test return error without server
  923. resp = client.loadPartitions(param);
  924. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  925. // test return error when client channel is shutdown
  926. client.close();
  927. resp = client.loadPartitions(param);
  928. assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
  929. }
  930. @Test
  931. void releasePartitionsParam() {
  932. // test throw exception with illegal input
  933. assertThrows(ParamException.class, () -> ReleasePartitionsParam.newBuilder()
  934. .withCollectionName("")
  935. .addPartitionName("partition1")
  936. .build()
  937. );
  938. assertThrows(ParamException.class, () -> ReleasePartitionsParam.newBuilder()
  939. .withCollectionName("collection1")
  940. .addPartitionName("")
  941. .build()
  942. );
  943. assertThrows(ParamException.class, () -> ReleasePartitionsParam.newBuilder()
  944. .withCollectionName("collection1")
  945. .build()
  946. );
  947. List<String> names = new ArrayList<>();
  948. names.add(null);
  949. assertThrows(NullPointerException.class, () -> ReleasePartitionsParam.newBuilder()
  950. .withCollectionName("collection1")
  951. .withPartitionNames(names)
  952. .build()
  953. );
  954. }
  955. @Test
  956. void releasePartitions() {
  957. ReleasePartitionsParam param = ReleasePartitionsParam.newBuilder()
  958. .withCollectionName("collection1")
  959. .addPartitionName("partition1")
  960. .build();
  961. testFuncByName("releasePartitions", param);
  962. }
  963. @Test
  964. void getPartitionStatisticsParam() {
  965. // test throw exception with illegal input
  966. assertThrows(ParamException.class, () -> GetPartitionStatisticsParam.newBuilder()
  967. .withCollectionName("")
  968. .withPartitionName("partition1")
  969. .withFlush(true)
  970. .build()
  971. );
  972. assertThrows(ParamException.class, () -> GetPartitionStatisticsParam.newBuilder()
  973. .withCollectionName("collection1")
  974. .withPartitionName("")
  975. .withFlush(false)
  976. .build()
  977. );
  978. }
  979. @Test
  980. void getPartitionStatistics() {
  981. GetPartitionStatisticsParam param = GetPartitionStatisticsParam.newBuilder()
  982. .withCollectionName("collection1")
  983. .withPartitionName("partition1")
  984. .build();
  985. testFuncByName("getPartitionStatistics", param);
  986. }
  987. @Test
  988. void showPartitionsParam() {
  989. // test throw exception with illegal input
  990. assertThrows(ParamException.class, () -> ShowPartitionsParam.newBuilder()
  991. .withCollectionName("")
  992. .addPartitionName("partition1")
  993. .build()
  994. );
  995. assertThrows(ParamException.class, () -> ShowPartitionsParam.newBuilder()
  996. .withCollectionName("collection1`")
  997. .addPartitionName("")
  998. .build()
  999. );
  1000. List<String> names = new ArrayList<>();
  1001. names.add(null);
  1002. assertThrows(NullPointerException.class, () -> ShowPartitionsParam.newBuilder()
  1003. .withCollectionName("collection1`")
  1004. .withPartitionNames(names)
  1005. .build()
  1006. );
  1007. // verify internal param
  1008. ShowPartitionsParam param = ShowPartitionsParam.newBuilder()
  1009. .withCollectionName("collection1`")
  1010. .build();
  1011. assertEquals(ShowType.All, param.getShowType());
  1012. param = ShowPartitionsParam.newBuilder()
  1013. .withCollectionName("collection1`")
  1014. .addPartitionName("partition1")
  1015. .build();
  1016. assertEquals(ShowType.InMemory, param.getShowType());
  1017. }
  1018. @Test
  1019. void showPartitions() {
  1020. ShowPartitionsParam param = ShowPartitionsParam.newBuilder()
  1021. .withCollectionName("collection1")
  1022. .build();
  1023. testFuncByName("showPartitions", param);
  1024. }
  1025. @Test
  1026. void createAliasParam() {
  1027. // test throw exception with illegal input
  1028. assertThrows(ParamException.class, () -> CreateAliasParam.newBuilder()
  1029. .withCollectionName("")
  1030. .withAlias("alias1")
  1031. .build()
  1032. );
  1033. assertThrows(ParamException.class, () -> CreateAliasParam.newBuilder()
  1034. .withCollectionName("collection1")
  1035. .withAlias("")
  1036. .build()
  1037. );
  1038. }
  1039. @Test
  1040. void createAlias() {
  1041. CreateAliasParam param = CreateAliasParam.newBuilder()
  1042. .withCollectionName("collection1")
  1043. .withAlias("alias1")
  1044. .build();
  1045. testFuncByName("createAlias", param);
  1046. }
  1047. @Test
  1048. void dropAliasParam() {
  1049. // test throw exception with illegal input
  1050. assertThrows(ParamException.class, () -> DropAliasParam.newBuilder()
  1051. .withAlias("")
  1052. .build()
  1053. );
  1054. }
  1055. @Test
  1056. void dropAlias() {
  1057. DropAliasParam param = DropAliasParam.newBuilder()
  1058. .withAlias("alias1")
  1059. .build();
  1060. testFuncByName("dropAlias", param);
  1061. }
  1062. @Test
  1063. void alterAliasParam() {
  1064. // test throw exception with illegal input
  1065. assertThrows(ParamException.class, () -> CreateAliasParam.newBuilder()
  1066. .withCollectionName("")
  1067. .withAlias("alias1")
  1068. .build()
  1069. );
  1070. assertThrows(ParamException.class, () -> CreateAliasParam.newBuilder()
  1071. .withCollectionName("collection1")
  1072. .withAlias("")
  1073. .build()
  1074. );
  1075. }
  1076. @Test
  1077. void alterAlias() {
  1078. AlterAliasParam param = AlterAliasParam.newBuilder()
  1079. .withCollectionName("collection1")
  1080. .withAlias("alias1")
  1081. .build();
  1082. testFuncByName("alterAlias", param);
  1083. }
  1084. @Test
  1085. void createIndexParam() {
  1086. // test throw exception with illegal input
  1087. assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
  1088. .withCollectionName("")
  1089. .withFieldName("field1")
  1090. .withIndexType(IndexType.IVF_FLAT)
  1091. .withMetricType(MetricType.L2)
  1092. .withExtraParam("dummy")
  1093. .build()
  1094. );
  1095. assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
  1096. .withCollectionName("collection1")
  1097. .withFieldName("")
  1098. .withIndexType(IndexType.IVF_FLAT)
  1099. .withMetricType(MetricType.L2)
  1100. .withExtraParam("dummy")
  1101. .build()
  1102. );
  1103. assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
  1104. .withCollectionName("collection1")
  1105. .withFieldName("field1")
  1106. .withIndexType(IndexType.INVALID)
  1107. .withMetricType(MetricType.L2)
  1108. .withExtraParam("dummy")
  1109. .build()
  1110. );
  1111. assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
  1112. .withCollectionName("collection1")
  1113. .withFieldName("field1")
  1114. .withIndexType(IndexType.IVF_FLAT)
  1115. .withMetricType(MetricType.INVALID)
  1116. .withExtraParam("dummy")
  1117. .build()
  1118. );
  1119. assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
  1120. .withCollectionName("collection1")
  1121. .withFieldName("field1")
  1122. .withIndexType(IndexType.IVF_FLAT)
  1123. .withMetricType(MetricType.L2)
  1124. .withSyncMode(Boolean.TRUE)
  1125. .withSyncWaitingInterval(-1L)
  1126. .build()
  1127. );
  1128. assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
  1129. .withCollectionName("collection1")
  1130. .withFieldName("field1")
  1131. .withIndexType(IndexType.IVF_FLAT)
  1132. .withMetricType(MetricType.L2)
  1133. .withSyncMode(Boolean.TRUE)
  1134. .withSyncWaitingInterval(Constant.MAX_WAITING_INDEX_INTERVAL + 1L)
  1135. .build()
  1136. );
  1137. assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
  1138. .withCollectionName("collection1")
  1139. .withFieldName("field1")
  1140. .withIndexType(IndexType.IVF_FLAT)
  1141. .withMetricType(MetricType.L2)
  1142. .withSyncMode(Boolean.TRUE)
  1143. .withSyncWaitingTimeout(0L)
  1144. .build()
  1145. );
  1146. }
  1147. @Test
  1148. void createIndex() {
  1149. // start mock server
  1150. MockMilvusServer server = startServer();
  1151. MilvusServiceClient client = startClient();
  1152. // createIndex() calls describeCollection() to check input
  1153. CollectionSchema schema = CollectionSchema.newBuilder()
  1154. .addFields(FieldSchema.newBuilder()
  1155. .setName("field1")
  1156. .setDataType(DataType.FloatVector)
  1157. .addTypeParams(KeyValuePair.newBuilder().setKey(Constant.VECTOR_DIM).setValue("256").build())
  1158. .build())
  1159. .build();
  1160. mockServerImpl.setDescribeCollectionResponse(DescribeCollectionResponse.newBuilder().setSchema(schema).build());
  1161. // test return ok for sync mode loading
  1162. mockServerImpl.setDescribeIndexResponse(DescribeIndexResponse.newBuilder()
  1163. .addIndexDescriptions(IndexDescription.newBuilder().setState(IndexState.InProgress).build())
  1164. .build());
  1165. // field doesn't exist
  1166. CreateIndexParam param = CreateIndexParam.newBuilder()
  1167. .withCollectionName("collection1")
  1168. .withFieldName("aaa")
  1169. .withIndexType(IndexType.IVF_FLAT)
  1170. .withMetricType(MetricType.L2)
  1171. .withExtraParam("dummy")
  1172. .withSyncMode(Boolean.TRUE)
  1173. .withSyncWaitingInterval(500L)
  1174. .withSyncWaitingTimeout(2L)
  1175. .build();
  1176. R<RpcStatus> resp = client.createIndex(param);
  1177. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1178. // index type doesn't match with data type
  1179. param = CreateIndexParam.newBuilder()
  1180. .withCollectionName("collection1")
  1181. .withFieldName("field1")
  1182. .withIndexType(IndexType.BIN_IVF_FLAT)
  1183. .withMetricType(MetricType.L2)
  1184. .withExtraParam("dummy")
  1185. .withSyncMode(Boolean.TRUE)
  1186. .withSyncWaitingInterval(500L)
  1187. .withSyncWaitingTimeout(2L)
  1188. .build();
  1189. resp = client.createIndex(param);
  1190. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1191. new Thread(() -> {
  1192. try {
  1193. TimeUnit.SECONDS.sleep(1);
  1194. mockServerImpl.setDescribeIndexResponse(DescribeIndexResponse.newBuilder()
  1195. .addIndexDescriptions(IndexDescription.newBuilder().setState(IndexState.Finished).build())
  1196. .build());
  1197. } catch (InterruptedException e) {
  1198. mockServerImpl.setDescribeIndexResponse(DescribeIndexResponse.newBuilder()
  1199. .addIndexDescriptions(IndexDescription.newBuilder().setState(IndexState.Finished).build())
  1200. .build());
  1201. }
  1202. }, "RefreshIndexState").start();
  1203. param = CreateIndexParam.newBuilder()
  1204. .withCollectionName("collection1")
  1205. .withFieldName("field1")
  1206. .withIndexType(IndexType.IVF_FLAT)
  1207. .withMetricType(MetricType.L2)
  1208. .withExtraParam("dummy")
  1209. .withSyncMode(Boolean.TRUE)
  1210. .withSyncWaitingInterval(500L)
  1211. .withSyncWaitingTimeout(2L)
  1212. .build();
  1213. // test return ok with correct input
  1214. resp = client.createIndex(param);
  1215. assertEquals(R.Status.Success.getCode(), resp.getStatus());
  1216. // stop mock server
  1217. server.stop();
  1218. // test return error without server
  1219. resp = client.createIndex(param);
  1220. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1221. // test return error when client channel is shutdown
  1222. client.close();
  1223. resp = client.createIndex(param);
  1224. assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
  1225. }
  1226. @Test
  1227. void describeIndexParam() {
  1228. DescribeIndexParam param = DescribeIndexParam.newBuilder()
  1229. .withCollectionName("collection1")
  1230. .withIndexName("dummy")
  1231. .build();
  1232. assertEquals("dummy", param.getIndexName());
  1233. assertThrows(ParamException.class, () -> DescribeIndexParam.newBuilder()
  1234. .withCollectionName("")
  1235. .withIndexName("field1")
  1236. .build()
  1237. );
  1238. }
  1239. @Test
  1240. void describeIndex() {
  1241. DescribeIndexParam param = DescribeIndexParam.newBuilder()
  1242. .withCollectionName("collection1")
  1243. .withIndexName("idx")
  1244. .build();
  1245. testFuncByName("describeIndex", param);
  1246. }
  1247. @Test
  1248. void getIndexStateParam() {
  1249. // test throw exception with illegal input
  1250. GetIndexStateParam param = GetIndexStateParam.newBuilder()
  1251. .withCollectionName("collection1")
  1252. .withIndexName("")
  1253. .build();
  1254. assertEquals(Constant.DEFAULT_INDEX_NAME, param.getIndexName());
  1255. param = GetIndexStateParam.newBuilder()
  1256. .withCollectionName("collection1")
  1257. .withIndexName("dummy")
  1258. .build();
  1259. assertEquals("dummy", param.getIndexName());
  1260. assertThrows(ParamException.class, () -> GetIndexStateParam.newBuilder()
  1261. .withCollectionName("")
  1262. .withIndexName("field1")
  1263. .build()
  1264. );
  1265. }
  1266. @Test
  1267. void getIndexState() {
  1268. GetIndexStateParam param = GetIndexStateParam.newBuilder()
  1269. .withCollectionName("collection1")
  1270. .withIndexName("idx")
  1271. .build();
  1272. testFuncByName("getIndexState", param);
  1273. }
  1274. @Test
  1275. void getIndexBuildProgressParam() {
  1276. // test throw exception with illegal input
  1277. assertThrows(ParamException.class, () -> GetIndexBuildProgressParam.newBuilder()
  1278. .withCollectionName("")
  1279. .build()
  1280. );
  1281. }
  1282. @Test
  1283. void getIndexBuildProgress() {
  1284. GetIndexBuildProgressParam param = GetIndexBuildProgressParam.newBuilder()
  1285. .withCollectionName("collection1")
  1286. .build();
  1287. testFuncByName("getIndexBuildProgress", param);
  1288. }
  1289. @Test
  1290. void dropIndexParam() {
  1291. // test throw exception with illegal input
  1292. DropIndexParam param = DropIndexParam.newBuilder()
  1293. .withCollectionName("collection1")
  1294. .withIndexName("")
  1295. .build();
  1296. assertEquals(Constant.DEFAULT_INDEX_NAME, param.getIndexName());
  1297. param = DropIndexParam.newBuilder()
  1298. .withCollectionName("collection1")
  1299. .withIndexName("dummy")
  1300. .build();
  1301. assertEquals("dummy", param.getIndexName());
  1302. assertThrows(ParamException.class, () -> DropIndexParam.newBuilder()
  1303. .withCollectionName("")
  1304. .withIndexName("field1")
  1305. .build()
  1306. );
  1307. }
  1308. @Test
  1309. void dropIndex() {
  1310. // start mock server
  1311. MockMilvusServer server = startServer();
  1312. MilvusServiceClient client = startClient();
  1313. DropIndexParam param = DropIndexParam.newBuilder()
  1314. .withCollectionName("collection1")
  1315. .withIndexName("idx")
  1316. .build();
  1317. // test return ok with correct input
  1318. mockServerImpl.setDescribeIndexResponse(DescribeIndexResponse.newBuilder()
  1319. .addIndexDescriptions(IndexDescription.newBuilder()
  1320. .setIndexName(param.getIndexName())
  1321. .setFieldName("fff")
  1322. .build())
  1323. .build());
  1324. R<RpcStatus> resp = client.dropIndex(param);
  1325. assertEquals(R.Status.Success.getCode(), resp.getStatus());
  1326. // stop mock server
  1327. server.stop();
  1328. // test return error without server
  1329. resp = client.dropIndex(param);
  1330. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1331. // test return error when client channel is shutdown
  1332. client.close();
  1333. resp = client.dropIndex(param);
  1334. assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
  1335. }
  1336. @Test
  1337. void insertParam() {
  1338. // test throw exception with illegal input
  1339. List<InsertParam.Field> fields = new ArrayList<>();
  1340. // collection is empty
  1341. assertThrows(ParamException.class, () -> InsertParam.newBuilder()
  1342. .withCollectionName("")
  1343. .withFields(fields)
  1344. .build()
  1345. );
  1346. // fields is empty
  1347. assertThrows(ParamException.class, () -> InsertParam.newBuilder()
  1348. .withCollectionName("collection1")
  1349. .withFields(fields)
  1350. .build()
  1351. );
  1352. // field is null
  1353. fields.add(null);
  1354. assertThrows(ParamException.class, () -> InsertParam.newBuilder()
  1355. .withCollectionName("collection1")
  1356. .withFields(fields)
  1357. .build()
  1358. );
  1359. // field name is empty
  1360. fields.clear();
  1361. List<Long> ids = new ArrayList<>();
  1362. fields.add(new InsertParam.Field("", ids));
  1363. assertThrows(ParamException.class, () -> InsertParam.newBuilder()
  1364. .withCollectionName("collection1")
  1365. .withFields(fields)
  1366. .build()
  1367. );
  1368. // field row count is 0
  1369. fields.clear();
  1370. fields.add(new InsertParam.Field("field1", ids));
  1371. assertThrows(ParamException.class, () -> InsertParam.newBuilder()
  1372. .withCollectionName("collection1")
  1373. .withFields(fields)
  1374. .build()
  1375. );
  1376. // field row count not equal
  1377. fields.clear();
  1378. List<Long> ages = Arrays.asList(1L, 2L);
  1379. fields.add(new InsertParam.Field("field1", ages));
  1380. List<Integer> ports = Arrays.asList(1, 2, 3);
  1381. fields.add(new InsertParam.Field("field2", ports));
  1382. assertThrows(ParamException.class, () -> InsertParam.newBuilder()
  1383. .withCollectionName("collection1")
  1384. .withFields(fields)
  1385. .build()
  1386. );
  1387. }
  1388. @Test
  1389. void insert() {
  1390. // prepare schema
  1391. HashMap<String, FieldSchema> fieldsSchema = new HashMap<>();
  1392. fieldsSchema.put("field0", ParamUtils.ConvertField(FieldType.newBuilder()
  1393. .withName("field0")
  1394. .withDataType(DataType.Int64)
  1395. .withAutoID(false)
  1396. .withPrimaryKey(true)
  1397. .build()));
  1398. fieldsSchema.put("field1", ParamUtils.ConvertField(FieldType.newBuilder()
  1399. .withName("field1")
  1400. .withDataType(DataType.Int32)
  1401. .build()));
  1402. fieldsSchema.put("field2", ParamUtils.ConvertField(FieldType.newBuilder()
  1403. .withName("field2")
  1404. .withDataType(DataType.Int16)
  1405. .build()));
  1406. fieldsSchema.put("field3", ParamUtils.ConvertField(FieldType.newBuilder()
  1407. .withName("field3")
  1408. .withDataType(DataType.Int8)
  1409. .build()));
  1410. fieldsSchema.put("field4", ParamUtils.ConvertField(FieldType.newBuilder()
  1411. .withName("field4")
  1412. .withDataType(DataType.Bool)
  1413. .build()));
  1414. fieldsSchema.put("field5", ParamUtils.ConvertField(FieldType.newBuilder()
  1415. .withName("field5")
  1416. .withDataType(DataType.Float)
  1417. .build()));
  1418. fieldsSchema.put("field6", ParamUtils.ConvertField(FieldType.newBuilder()
  1419. .withName("field6")
  1420. .withDataType(DataType.Double)
  1421. .build()));
  1422. fieldsSchema.put("field7", ParamUtils.ConvertField(FieldType.newBuilder()
  1423. .withName("field7")
  1424. .withDataType(DataType.VarChar)
  1425. .withMaxLength(20)
  1426. .build()));
  1427. fieldsSchema.put("field8", ParamUtils.ConvertField(FieldType.newBuilder()
  1428. .withName("field8")
  1429. .withDataType(DataType.FloatVector)
  1430. .withDimension(2)
  1431. .build()));
  1432. fieldsSchema.put("field9", ParamUtils.ConvertField(FieldType.newBuilder()
  1433. .withName("field9")
  1434. .withDataType(DataType.BinaryVector)
  1435. .withDimension(16)
  1436. .build()));
  1437. // prepare raw data
  1438. List<Long> ids = new ArrayList<>();
  1439. List<Integer> nVal = new ArrayList<>();
  1440. List<Boolean> bVal = new ArrayList<>();
  1441. List<Float> fVal = new ArrayList<>();
  1442. List<Double> dVal = new ArrayList<>();
  1443. List<String> sVal = new ArrayList<>();
  1444. List<ByteBuffer> bVectors = new ArrayList<>();
  1445. List<List<Float>> fVectors = new ArrayList<>();
  1446. int rowCount = 3;
  1447. for (int i = 0; i < rowCount; ++i) {
  1448. ids.add((long) i);
  1449. nVal.add(i);
  1450. bVal.add(Boolean.TRUE);
  1451. fVal.add(0.5f);
  1452. dVal.add(1.0);
  1453. sVal.add(String.valueOf(i));
  1454. ByteBuffer buf = ByteBuffer.allocate(2);
  1455. buf.put((byte) 1);
  1456. buf.put((byte) 2);
  1457. bVectors.add(buf);
  1458. List<Float> vec = Arrays.asList(0.1f, 0.2f);
  1459. fVectors.add(vec);
  1460. }
  1461. CollectionSchema.Builder colBuilder = CollectionSchema.newBuilder();
  1462. colBuilder.addFields(fieldsSchema.get("field0"));
  1463. colBuilder.addFields(fieldsSchema.get("field1"));
  1464. colBuilder.addFields(fieldsSchema.get("field2"));
  1465. colBuilder.addFields(fieldsSchema.get("field3"));
  1466. colBuilder.addFields(fieldsSchema.get("field4"));
  1467. colBuilder.addFields(fieldsSchema.get("field5"));
  1468. colBuilder.addFields(fieldsSchema.get("field6"));
  1469. colBuilder.addFields(fieldsSchema.get("field7"));
  1470. colBuilder.addFields(fieldsSchema.get("field8"));
  1471. colBuilder.addFields(fieldsSchema.get("field9"));
  1472. List<InsertParam.Field> fields = new ArrayList<>();
  1473. fields.add(new InsertParam.Field("field0", ids));
  1474. fields.add(new InsertParam.Field("field1", nVal));
  1475. fields.add(new InsertParam.Field("field2", nVal));
  1476. fields.add(new InsertParam.Field("field3", nVal));
  1477. fields.add(new InsertParam.Field("field4", bVal));
  1478. fields.add(new InsertParam.Field("field5", fVal));
  1479. fields.add(new InsertParam.Field("field6", dVal));
  1480. fields.add(new InsertParam.Field("field7", sVal));
  1481. fields.add(new InsertParam.Field("field8", fVectors));
  1482. fields.add(new InsertParam.Field("field9", bVectors));
  1483. InsertParam param = InsertParam.newBuilder()
  1484. .withCollectionName("collection1")
  1485. .withPartitionName("partition1")
  1486. .withFields(fields)
  1487. .build();
  1488. {
  1489. // start mock server
  1490. MockMilvusServer server = startServer();
  1491. MilvusServiceClient client = startClient();
  1492. // test return ok with correct input
  1493. mockServerImpl.setDescribeCollectionResponse(DescribeCollectionResponse.newBuilder()
  1494. .setCollectionID(1L)
  1495. .setShardsNum(2)
  1496. .setSchema(colBuilder.build())
  1497. .build());
  1498. R<MutationResult> resp = client.insert(param);
  1499. assertEquals(R.Status.Success.getCode(), resp.getStatus());
  1500. // test return error with wrong int64 type
  1501. InsertParam.Builder paramBuilder = InsertParam.newBuilder();
  1502. paramBuilder.withCollectionName("collection1");
  1503. fields.set(0, new InsertParam.Field("field0", nVal));
  1504. resp = client.insert(paramBuilder.withFields(fields).build());
  1505. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1506. // test return error with wrong int32 type
  1507. fields.set(0, new InsertParam.Field("field0", ids));
  1508. fields.set(1, new InsertParam.Field("field1", ids));
  1509. resp = client.insert(paramBuilder.withFields(fields).build());
  1510. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1511. // test return error with wrong int16 type
  1512. fields.set(1, new InsertParam.Field("field1", nVal));
  1513. fields.set(2, new InsertParam.Field("field2", ids));
  1514. resp = client.insert(paramBuilder.withFields(fields).build());
  1515. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1516. // test return error with wrong int8 type
  1517. fields.set(2, new InsertParam.Field("field2", nVal));
  1518. fields.set(3, new InsertParam.Field("field3", ids));
  1519. resp = client.insert(paramBuilder.withFields(fields).build());
  1520. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1521. // test return error with wrong bool type
  1522. fields.set(3, new InsertParam.Field("field3", nVal));
  1523. fields.set(4, new InsertParam.Field("field4", nVal));
  1524. resp = client.insert(paramBuilder.withFields(fields).build());
  1525. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1526. // test return error with wrong float type
  1527. fields.set(4, new InsertParam.Field("field4", bVal));
  1528. fields.set(5, new InsertParam.Field("field5", bVal));
  1529. resp = client.insert(paramBuilder.withFields(fields).build());
  1530. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1531. // test return error with wrong double type
  1532. fields.set(5, new InsertParam.Field("field5", fVal));
  1533. fields.set(6, new InsertParam.Field("field6", nVal));
  1534. resp = client.insert(paramBuilder.withFields(fields).build());
  1535. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1536. // test return error with wrong varchar type
  1537. fields.set(6, new InsertParam.Field("field6", dVal));
  1538. fields.set(7, new InsertParam.Field("field7", nVal));
  1539. resp = client.insert(paramBuilder.withFields(fields).build());
  1540. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1541. // test return error with wrong float vector type
  1542. fields.set(7, new InsertParam.Field("field7", sVal));
  1543. fields.set(8, new InsertParam.Field("field8", nVal));
  1544. resp = client.insert(paramBuilder.withFields(fields).build());
  1545. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1546. // test throw exception when row count is not equal
  1547. List<List<Long>> fakeList = new ArrayList<>();
  1548. fakeList.add(ids);
  1549. fields.set(8, new InsertParam.Field("field8", fakeList));
  1550. assertThrows(ParamException.class, () -> paramBuilder.withFields(fields).build());
  1551. // test return error with wrong dimension of float vector
  1552. fVectors.remove(rowCount - 1);
  1553. fVectors.add(fVal);
  1554. fields.set(8, new InsertParam.Field("field8", fVectors));
  1555. resp = client.insert(paramBuilder.withFields(fields).build());
  1556. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1557. // test return error with wrong binary vector type
  1558. fVectors.remove(rowCount - 1);
  1559. fVectors.add(Arrays.asList(0.1f, 0.2f));
  1560. fields.set(8, new InsertParam.Field("field8", fVectors));
  1561. fields.set(9, new InsertParam.Field("field9", nVal));
  1562. resp = client.insert(paramBuilder.withFields(fields).build());
  1563. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1564. // test throw exception when row count is not equal
  1565. List<ByteBuffer> fakeList2 = new ArrayList<>();
  1566. fakeList2.add(ByteBuffer.allocate(2));
  1567. fields.set(9, new InsertParam.Field("field9", fakeList2));
  1568. assertThrows(ParamException.class, () -> paramBuilder.withFields(fields).build());
  1569. // test return error with wrong dimension of binary vector
  1570. bVectors.remove(rowCount - 1);
  1571. bVectors.add(ByteBuffer.allocate(1));
  1572. fields.set(9, new InsertParam.Field("field9", bVectors));
  1573. resp = client.insert(paramBuilder.withFields(fields).build());
  1574. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1575. // test return error without server
  1576. server.stop();
  1577. resp = client.insert(param);
  1578. assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
  1579. // test return error when client channel is shutdown
  1580. client.close();
  1581. resp = client.insert(param);
  1582. assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
  1583. // stop mock server
  1584. server.stop();
  1585. }
  1586. {
  1587. // start mock server
  1588. MockMilvusServer server = startServer();
  1589. MilvusServiceClient client = startClient();
  1590. // test return ok with insertAsync
  1591. try {
  1592. ListenableFuture<R<MutationResult>> respFuture = client.insertAsync(param);
  1593. R<MutationResult> response = respFuture.get();
  1594. assertEquals(R.Status.Success.getCode(), response.getStatus());
  1595. } catch (InterruptedException | ExecutionException e) {
  1596. e.printStackTrace();
  1597. }
  1598. // stop mock server
  1599. server.stop();
  1600. // test return error without server
  1601. try {
  1602. ListenableFuture<R<MutationResult>> respFuture = client.insertAsync(param);
  1603. R<MutationResult> response = respFuture.get();
  1604. assertNotEquals(R.Status.Success.getCode(), response.getStatus());
  1605. } catch (InterruptedException | ExecutionException e) {
  1606. e.printStackTrace();
  1607. }
  1608. // test return error when client channel is shutdown
  1609. client.close();
  1610. try {
  1611. ListenableFuture<R<MutationResult>> respFuture = client.insertAsync(param);
  1612. R<MutationResult> response = respFuture.get();
  1613. assertNotEquals(R.Status.Success.getCode(), response.getStatus());
  1614. } catch (InterruptedException | ExecutionException e) {
  1615. e.printStackTrace();
  1616. }
  1617. // stop mock server
  1618. server.stop();
  1619. }
  1620. }
  1621. @Test
  1622. void deleteParam() {
  1623. // test throw exception with illegal input
  1624. assertThrows(ParamException.class, () -> DeleteParam.newBuilder()
  1625. .withCollectionName("")
  1626. .withExpr("dummy")
  1627. .build()
  1628. );
  1629. assertThrows(ParamException.class, () -> DeleteParam.newBuilder()
  1630. .withCollectionName("collection1")
  1631. .withExpr("")
  1632. .build()
  1633. );
  1634. }
  1635. @Test
  1636. void delete() {
  1637. DeleteParam param = DeleteParam.newBuilder()
  1638. .withCollectionName("collection1")
  1639. .withPartitionName("partition1")
  1640. .withExpr("dummy")
  1641. .build();
  1642. testFuncByName("delete", param);
  1643. }
  1644. // @Test
  1645. // void bulkload() {
  1646. // List<String> files = Collections.singletonList("f1");
  1647. // BulkloadParam param = BulkloadParam.newBuilder()
  1648. // .withCollectionName("collection1")
  1649. // .withPartitionName("partition1")
  1650. // .withRowBased(true)
  1651. // .addFile("dummy.json")
  1652. // .withFiles(files)
  1653. // .withBucket("myBucket")
  1654. // .build();
  1655. //
  1656. // testFuncByName("bulkload", param);
  1657. // }
  1658. //
  1659. // @Test
  1660. // void getBulkloadState() {
  1661. // GetBulkloadStateParam param = GetBulkloadStateParam.newBuilder()
  1662. // .withTaskID(100L)
  1663. // .build();
  1664. //
  1665. // testFuncByName("getBulkloadState", param);
  1666. // }
  1667. //
  1668. // @Test
  1669. // void listBulkloadTasks() {
  1670. // ListBulkloadTasksParam param = ListBulkloadTasksParam.newBuilder().build();
  1671. //
  1672. // testFuncByName("listBulkloadTasks", param);
  1673. // }
  1674. @Test
  1675. void searchParam() {
  1676. // test throw exception with illegal input
  1677. List<String> partitions = Collections.singletonList("partition1");
  1678. List<String> outputFields = Collections.singletonList("field1");
  1679. List<List<Float>> vectors = new ArrayList<>();
  1680. // target vector is empty
  1681. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1682. .withCollectionName("collection1")
  1683. .withPartitionNames(partitions)
  1684. .addPartitionName("p2")
  1685. .withParams("{}")
  1686. .withOutFields(outputFields)
  1687. .withVectorFieldName("field1")
  1688. .withMetricType(MetricType.IP)
  1689. .withTopK(5)
  1690. .withVectors(vectors)
  1691. .withExpr("dummy")
  1692. .build()
  1693. );
  1694. // collection name is empty
  1695. List<Float> vector1 = Collections.singletonList(0.1F);
  1696. vectors.add(vector1);
  1697. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1698. .withCollectionName("")
  1699. .withPartitionNames(partitions)
  1700. .withParams("{}")
  1701. .withOutFields(outputFields)
  1702. .withVectorFieldName("field1")
  1703. .withMetricType(MetricType.IP)
  1704. .withTopK(5)
  1705. .withVectors(vectors)
  1706. .withExpr("dummy")
  1707. .build()
  1708. );
  1709. // target field name is empty
  1710. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1711. .withCollectionName("collection1")
  1712. .withPartitionNames(partitions)
  1713. .withParams("{}")
  1714. .withOutFields(outputFields)
  1715. .withVectorFieldName("")
  1716. .withMetricType(MetricType.IP)
  1717. .withTopK(5)
  1718. .withVectors(vectors)
  1719. .withExpr("dummy")
  1720. .build()
  1721. );
  1722. // metric type is invalid
  1723. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1724. .withCollectionName("collection1")
  1725. .withPartitionNames(partitions)
  1726. .withParams("{}")
  1727. .withOutFields(outputFields)
  1728. .withVectorFieldName("field1")
  1729. .withMetricType(MetricType.INVALID)
  1730. .withTopK(5)
  1731. .withVectors(vectors)
  1732. .withExpr("dummy")
  1733. .build()
  1734. );
  1735. // illegal topk value
  1736. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1737. .withCollectionName("collection1")
  1738. .withPartitionNames(partitions)
  1739. .withParams("{}")
  1740. .withOutFields(outputFields)
  1741. .withVectorFieldName("field1")
  1742. .withMetricType(MetricType.IP)
  1743. .withTopK(0)
  1744. .withVectors(vectors)
  1745. .withExpr("dummy")
  1746. .build()
  1747. );
  1748. // target vector type must be Lst<Float> or ByteBuffer
  1749. List<String> fakeVectors1 = Collections.singletonList("fake");
  1750. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1751. .withCollectionName("collection1")
  1752. .withPartitionNames(partitions)
  1753. .withParams("{}")
  1754. .withOutFields(outputFields)
  1755. .withVectorFieldName("field1")
  1756. .withMetricType(MetricType.IP)
  1757. .withTopK(5)
  1758. .withVectors(fakeVectors1)
  1759. .withExpr("dummy")
  1760. .build()
  1761. );
  1762. // float vector field's value must be Lst<Float>
  1763. List<List<String>> fakeVectors2 = Collections.singletonList(fakeVectors1);
  1764. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1765. .withCollectionName("collection1")
  1766. .withPartitionNames(partitions)
  1767. .withParams("{}")
  1768. .withOutFields(outputFields)
  1769. .withVectorFieldName("field1")
  1770. .withMetricType(MetricType.IP)
  1771. .withTopK(5)
  1772. .withVectors(fakeVectors2)
  1773. .withExpr("dummy")
  1774. .build()
  1775. );
  1776. // float vector dimension not equal
  1777. List<Float> vector2 = Arrays.asList(0.1F, 0.2F);
  1778. vectors.add(vector2);
  1779. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1780. .withCollectionName("collection1")
  1781. .withPartitionNames(partitions)
  1782. .withParams("{}")
  1783. .withOutFields(outputFields)
  1784. .withVectorFieldName("field1")
  1785. .withMetricType(MetricType.IP)
  1786. .withTopK(5)
  1787. .withVectors(vectors)
  1788. .withExpr("dummy")
  1789. .build()
  1790. );
  1791. // binary vector dimension not equal
  1792. ByteBuffer buf1 = ByteBuffer.allocate(1);
  1793. buf1.put((byte) 1);
  1794. ByteBuffer buf2 = ByteBuffer.allocate(2);
  1795. buf2.put((byte) 1);
  1796. buf2.put((byte) 2);
  1797. List<ByteBuffer> binVectors = Arrays.asList(buf1, buf2);
  1798. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1799. .withCollectionName("collection1")
  1800. .withPartitionNames(partitions)
  1801. .withParams("{}")
  1802. .withOutFields(outputFields)
  1803. .withVectorFieldName("field1")
  1804. .withMetricType(MetricType.HAMMING)
  1805. .withTopK(5)
  1806. .withVectors(binVectors)
  1807. .withExpr("dummy")
  1808. .build()
  1809. );
  1810. // float vector metric type is illegal
  1811. List<List<Float>> vectors2 = Collections.singletonList(vector2);
  1812. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1813. .withCollectionName("collection1")
  1814. .withPartitionNames(partitions)
  1815. .withParams("{}")
  1816. .withOutFields(outputFields)
  1817. .withVectorFieldName("field1")
  1818. .withMetricType(MetricType.JACCARD)
  1819. .withTopK(5)
  1820. .withVectors(vectors2)
  1821. .withExpr("dummy")
  1822. .build()
  1823. );
  1824. // binary vector metric type is illegal
  1825. List<ByteBuffer> binVectors2 = Collections.singletonList(buf2);
  1826. assertThrows(ParamException.class, () -> SearchParam.newBuilder()
  1827. .withCollectionName("collection1")
  1828. .withPartitionNames(partitions)
  1829. .withParams("{}")
  1830. .withOutFields(outputFields)
  1831. .withVectorFieldName("field1")
  1832. .withMetricType(MetricType.IP)
  1833. .withTopK(5)
  1834. .withVectors(binVectors2)
  1835. .withExpr("dummy")
  1836. .build()
  1837. );
  1838. // succeed float vector case
  1839. assertDoesNotThrow(() -> SearchParam.newBuilder()
  1840. .withCollectionName("collection1")
  1841. .withPartitionNames(partitions)
  1842. .withParams("{}")
  1843. .withOutFields(outputFields)
  1844. .withVectorFieldName("field1")
  1845. .withMetricType(MetricType.L2)
  1846. .withTopK(5)
  1847. .withVectors(vectors2)
  1848. .withExpr("dummy")
  1849. .withIgnoreGrowing(Boolean.TRUE)
  1850. .build()
  1851. );
  1852. // succeed binary vector case
  1853. assertDoesNotThrow(() -> SearchParam.newBuilder()
  1854. .withCollectionName("collection1")
  1855. .withPartitionNames(partitions)
  1856. .withParams("{}")
  1857. .withOutFields(outputFields)
  1858. .withVectorFieldName("field1")
  1859. .withMetricType(MetricType.HAMMING)
  1860. .withTopK(5)
  1861. .withVectors(binVectors2)
  1862. .withExpr("dummy")
  1863. .build()
  1864. );
  1865. // param is not json format
  1866. assertDoesNotThrow(() -> SearchParam.newBuilder()
  1867. .withCollectionName("collection1")
  1868. .withPartitionNames(partitions)
  1869. .withParams("dummy")
  1870. .withOutFields(outputFields)
  1871. .withVectorFieldName("field1")
  1872. .withMetricType(MetricType.L2)
  1873. .withTopK(5)
  1874. .withVectors(vectors2)
  1875. .withExpr("dummy")
  1876. .build()
  1877. );
  1878. }
  1879. @Test
  1880. void search() {
  1881. List<String> partitions = Collections.singletonList("partition1");
  1882. List<String> outputFields = Collections.singletonList("field2");
  1883. List<List<Float>> vectors = new ArrayList<>();
  1884. List<Float> vector1 = Arrays.asList(0.1f, 0.2f);
  1885. vectors.add(vector1);
  1886. SearchParam param = SearchParam.newBuilder()
  1887. .withCollectionName("collection1")
  1888. .withPartitionNames(partitions)
  1889. .withParams("{}")
  1890. .withOutFields(outputFields)
  1891. .addOutField("f2")
  1892. .withVectorFieldName("field1")
  1893. .withMetricType(MetricType.IP)
  1894. .withTopK(5)
  1895. .withVectors(vectors)
  1896. .withExpr("dummy")
  1897. .withRoundDecimal(5)
  1898. .withConsistencyLevel(ConsistencyLevelEnum.EVENTUALLY)
  1899. .build();
  1900. testFuncByName("search", param);
  1901. List<ByteBuffer> bVectors = new ArrayList<>();
  1902. ByteBuffer buf = ByteBuffer.allocate(2);
  1903. buf.put((byte) 1);
  1904. buf.put((byte) 2);
  1905. bVectors.add(buf);
  1906. param = SearchParam.newBuilder()
  1907. .withCollectionName("collection1")
  1908. .withPartitionNames(partitions)
  1909. .withParams("{}")
  1910. .withOutFields(outputFields)
  1911. .withVectorFieldName("field1")
  1912. .withMetricType(MetricType.HAMMING)
  1913. .withTopK(5)
  1914. .withVectors(bVectors)
  1915. .withExpr("dummy")
  1916. .build();
  1917. testFuncByName("search", param);
  1918. testAsyncFuncByName("searchAsync", param);
  1919. }
  1920. @Test
  1921. void queryParam() {
  1922. // test throw exception with illegal input
  1923. List<String> partitions = Collections.singletonList("partition1");
  1924. List<String> outputFields = Collections.singletonList("field1");
  1925. // empty collection name
  1926. assertThrows(ParamException.class, () -> QueryParam.newBuilder()
  1927. .withCollectionName("")
  1928. .withPartitionNames(partitions)
  1929. .addPartitionName("p2")
  1930. .withOutFields(outputFields)
  1931. .addOutField("f2")
  1932. .withExpr("dummy")
  1933. .build()
  1934. );
  1935. // negative topk
  1936. assertThrows(ParamException.class, () -> QueryParam.newBuilder()
  1937. .withCollectionName("collection1")
  1938. .withPartitionNames(partitions)
  1939. .withOutFields(outputFields)
  1940. .withExpr("dummy")
  1941. .withLimit(-1L)
  1942. .build()
  1943. );
  1944. // negative offset
  1945. assertThrows(ParamException.class, () -> QueryParam.newBuilder()
  1946. .withCollectionName("collection1")
  1947. .withPartitionNames(partitions)
  1948. .withOutFields(outputFields)
  1949. .withExpr("dummy")
  1950. .withOffset(-1L)
  1951. .build()
  1952. );
  1953. // success
  1954. assertDoesNotThrow(() -> QueryParam.newBuilder()
  1955. .withCollectionName("collection1")
  1956. .withPartitionNames(partitions)
  1957. .withOutFields(outputFields)
  1958. .withExpr("dummy")
  1959. .withOffset(1L)
  1960. .withLimit(1L)
  1961. .withIgnoreGrowing(Boolean.TRUE)
  1962. .build()
  1963. );
  1964. }
  1965. @Test
  1966. void query() {
  1967. List<String> partitions = Collections.singletonList("partition1");
  1968. List<String> outputFields = Collections.singletonList("field2");
  1969. QueryParam param = QueryParam.newBuilder()
  1970. .withCollectionName("collection1")
  1971. .withPartitionNames(partitions)
  1972. .withOutFields(outputFields)
  1973. .addOutField("d1")
  1974. .withExpr("dummy")
  1975. .withConsistencyLevel(ConsistencyLevelEnum.EVENTUALLY)
  1976. .build();
  1977. testFuncByName("query", param);
  1978. testAsyncFuncByName("queryAsync", param);
  1979. }
  1980. @Test
  1981. void getMetricsParam() {
  1982. // test throw exception with illegal input
  1983. assertThrows(ParamException.class, () -> GetMetricsParam.newBuilder()
  1984. .withRequest("")
  1985. .build()
  1986. );
  1987. }
  1988. @Test
  1989. void getMetrics() {
  1990. GetMetricsParam param = GetMetricsParam.newBuilder()
  1991. .withRequest("{}")
  1992. .build();
  1993. testFuncByName("getMetrics", param);
  1994. }
  1995. @Test
  1996. void getFlushStateParam() {
  1997. // test throw exception with illegal input
  1998. assertThrows(ParamException.class, () -> GetFlushStateParam.newBuilder()
  1999. .build()
  2000. );
  2001. }
  2002. @Test
  2003. void getFlushState() {
  2004. List<Long> ids = Arrays.asList(1L, 2L);
  2005. GetFlushStateParam param = GetFlushStateParam.newBuilder()
  2006. .withCollectionName("dummy")
  2007. .withFlushTs(100L)
  2008. .build();
  2009. testFuncByName("getFlushState", param);
  2010. }
  2011. @Test
  2012. void getPersistentSegmentInfoParam() {
  2013. // test throw exception with illegal input
  2014. assertThrows(ParamException.class, () -> GetPersistentSegmentInfoParam.newBuilder()
  2015. .withCollectionName("")
  2016. .build()
  2017. );
  2018. }
  2019. @Test
  2020. void getPersistentSegmentInfo() {
  2021. GetPersistentSegmentInfoParam param = GetPersistentSegmentInfoParam.newBuilder()
  2022. .withCollectionName("collection1")
  2023. .build();
  2024. testFuncByName("getPersistentSegmentInfo", param);
  2025. }
  2026. @Test
  2027. void getQuerySegmentInfoParam() {
  2028. // test throw exception with illegal input
  2029. assertThrows(ParamException.class, () -> GetQuerySegmentInfoParam
  2030. .newBuilder()
  2031. .withCollectionName("")
  2032. .build()
  2033. );
  2034. }
  2035. @Test
  2036. void getQuerySegmentInfo() {
  2037. GetQuerySegmentInfoParam param = GetQuerySegmentInfoParam.newBuilder()
  2038. .withCollectionName("collection1")
  2039. .build();
  2040. testFuncByName("getQuerySegmentInfo", param);
  2041. }
  2042. @Test
  2043. void getReplicasParam() {
  2044. // test throw exception with illegal input
  2045. assertThrows(ParamException.class, () -> GetQuerySegmentInfoParam
  2046. .newBuilder()
  2047. .withCollectionName("")
  2048. .build()
  2049. );
  2050. }
  2051. @Test
  2052. void getReplicas() {
  2053. GetReplicasParam param = GetReplicasParam.newBuilder()
  2054. .withCollectionName("collection1")
  2055. .build();
  2056. testFuncByName("getReplicas", param);
  2057. }
  2058. @Test
  2059. void loadBalanceParam() {
  2060. // test throw exception with illegal input
  2061. assertThrows(ParamException.class, () -> LoadBalanceParam
  2062. .newBuilder()
  2063. .withSourceNodeID(1L)
  2064. .withDestinationNodeID(Arrays.asList(2L, 3L))
  2065. .addDestinationNodeID(4L)
  2066. .build()
  2067. );
  2068. assertThrows(ParamException.class, () -> LoadBalanceParam
  2069. .newBuilder()
  2070. .withSourceNodeID(1L)
  2071. .withSegmentIDs(Arrays.asList(2L, 3L))
  2072. .addSegmentID(4L)
  2073. .build()
  2074. );
  2075. }
  2076. @Test
  2077. void loadBalance() {
  2078. LoadBalanceParam param = LoadBalanceParam.newBuilder()
  2079. .withSourceNodeID(1L)
  2080. .addDestinationNodeID(2L)
  2081. .addSegmentID(3L)
  2082. .build();
  2083. testFuncByName("loadBalance", param);
  2084. }
  2085. @Test
  2086. void getCompactionState() {
  2087. GetCompactionStateParam param = GetCompactionStateParam.newBuilder()
  2088. .withCompactionID(1L)
  2089. .build();
  2090. testFuncByName("getCompactionState", param);
  2091. }
  2092. @Test
  2093. void manualCompact() {
  2094. ManualCompactParam param = ManualCompactParam.newBuilder()
  2095. .withCollectionName("collection1")
  2096. .build();
  2097. testFuncByName("manualCompact", param);
  2098. }
  2099. @Test
  2100. void getCompactionStateWithPlans() {
  2101. GetCompactionPlansParam param = GetCompactionPlansParam.newBuilder()
  2102. .withCompactionID(1L)
  2103. .build();
  2104. testFuncByName("getCompactionStateWithPlans", param);
  2105. }
  2106. @Test
  2107. void createCredentialParam() {
  2108. assertThrows(ParamException.class, () -> CreateCredentialParam
  2109. .newBuilder()
  2110. .withUsername(" ")
  2111. .withPassword("password")
  2112. .build()
  2113. );
  2114. assertThrows(ParamException.class, () -> CreateCredentialParam
  2115. .newBuilder()
  2116. .withUsername("username")
  2117. .withPassword(" ")
  2118. .build()
  2119. );
  2120. }
  2121. @Test
  2122. void createCredential() {
  2123. testFuncByName("createCredential", CreateCredentialParam
  2124. .newBuilder()
  2125. .withUsername("username")
  2126. .withPassword("password")
  2127. .build()
  2128. );
  2129. }
  2130. @Test
  2131. void updateCredentialParam() {
  2132. assertThrows(ParamException.class, () -> UpdateCredentialParam
  2133. .newBuilder()
  2134. .withUsername(" ")
  2135. .withOldPassword("oldPassword")
  2136. .withNewPassword("newPassword")
  2137. .build()
  2138. );
  2139. assertDoesNotThrow(() -> UpdateCredentialParam
  2140. .newBuilder()
  2141. .withUsername("username")
  2142. .withOldPassword("")
  2143. .withNewPassword("newPassword")
  2144. .build()
  2145. );
  2146. assertThrows(ParamException.class, () -> UpdateCredentialParam
  2147. .newBuilder()
  2148. .withUsername("username")
  2149. .withOldPassword("oldPassword")
  2150. .withNewPassword(" ")
  2151. .build()
  2152. );
  2153. }
  2154. @Test
  2155. void updateCredential() {
  2156. testFuncByName("updateCredential", UpdateCredentialParam
  2157. .newBuilder()
  2158. .withUsername("username")
  2159. .withOldPassword("oldPassword")
  2160. .withNewPassword("newPassword")
  2161. .build()
  2162. );
  2163. }
  2164. @Test
  2165. void deleteCredentialParam() {
  2166. assertThrows(ParamException.class, () -> DeleteCredentialParam
  2167. .newBuilder()
  2168. .withUsername(" ")
  2169. .build()
  2170. );
  2171. }
  2172. @Test
  2173. void deleteCredential() {
  2174. testFuncByName("deleteCredential", DeleteCredentialParam
  2175. .newBuilder()
  2176. .withUsername("username")
  2177. .build()
  2178. );
  2179. }
  2180. @Test
  2181. void listCredUsers() {
  2182. testFuncByName("listCredUsers", ListCredUsersParam
  2183. .newBuilder()
  2184. .build()
  2185. );
  2186. }
  2187. @Test
  2188. void getLoadingProgress() {
  2189. List<String> partitions = Collections.singletonList("partition1");
  2190. testFuncByName("getLoadingProgress", GetLoadingProgressParam
  2191. .newBuilder()
  2192. .withCollectionName("dummy")
  2193. .withPartitionNames(partitions)
  2194. .build()
  2195. );
  2196. }
  2197. @Test
  2198. void getLoadState() {
  2199. List<String> partitions = Collections.singletonList("partition1");
  2200. testFuncByName("getLoadState", GetLoadStateParam
  2201. .newBuilder()
  2202. .withCollectionName("dummy")
  2203. .withPartitionNames(partitions)
  2204. .build()
  2205. );
  2206. }
  2207. ////////////////////////////////////////////////////////////////////////////////////
  2208. // Response wrapper test
  2209. private void testScalarField(ScalarField field, DataType type, long rowCount) {
  2210. FieldData fieldData = FieldData.newBuilder()
  2211. .setFieldName("scalar")
  2212. .setFieldId(1L)
  2213. .setType(type)
  2214. .setScalars(field)
  2215. .build();
  2216. FieldDataWrapper wrapper = new FieldDataWrapper(fieldData);
  2217. assertEquals(rowCount, wrapper.getRowCount());
  2218. List<?> data = wrapper.getFieldData();
  2219. assertEquals(rowCount, data.size());
  2220. assertThrows(IllegalResponseException.class, wrapper::getDim);
  2221. }
  2222. @Test
  2223. void testDescCollResponseWrapper() {
  2224. String collName = "test";
  2225. String collDesc = "test col";
  2226. long collId = 100;
  2227. int shardNum = 10;
  2228. long utcTs = 9999;
  2229. List<String> aliases = Collections.singletonList("a1");
  2230. String fieldName = "f1";
  2231. String fieldDesc = "f1 field";
  2232. final boolean autoId = false;
  2233. final boolean primaryKey = true;
  2234. DataType dt = DataType.Double;
  2235. int dim = 256;
  2236. KeyValuePair kv = KeyValuePair.newBuilder()
  2237. .setKey(Constant.VECTOR_DIM).setValue(String.valueOf(dim)).build();
  2238. FieldSchema field = FieldSchema.newBuilder()
  2239. .setName(fieldName)
  2240. .setDescription(fieldDesc)
  2241. .setAutoID(autoId)
  2242. .setIsPrimaryKey(primaryKey)
  2243. .setDataType(dt)
  2244. .addTypeParams(kv)
  2245. .build();
  2246. CollectionSchema schema = CollectionSchema.newBuilder()
  2247. .setName(collName)
  2248. .setDescription(collDesc)
  2249. .addFields(field)
  2250. .build();
  2251. DescribeCollectionResponse response = DescribeCollectionResponse.newBuilder()
  2252. .setCollectionID(collId)
  2253. .addAllAliases(aliases)
  2254. .setShardsNum(shardNum)
  2255. .setCreatedUtcTimestamp(utcTs)
  2256. .setSchema(schema)
  2257. .build();
  2258. DescCollResponseWrapper wrapper = new DescCollResponseWrapper(response);
  2259. assertEquals(collName, wrapper.getCollectionName());
  2260. assertEquals(collDesc, wrapper.getCollectionDescription());
  2261. assertEquals(collId, wrapper.getCollectionID());
  2262. assertEquals(shardNum, wrapper.getShardNumber());
  2263. assertEquals(aliases.size(), wrapper.getAliases().size());
  2264. assertEquals(utcTs, wrapper.getCreatedUtcTimestamp());
  2265. assertEquals(1, wrapper.getFields().size());
  2266. assertNull(wrapper.getFieldByName(""));
  2267. FieldType ft = wrapper.getFieldByName(fieldName);
  2268. assertEquals(fieldName, ft.getName());
  2269. assertEquals(fieldDesc, ft.getDescription());
  2270. assertEquals(dt, ft.getDataType());
  2271. assertEquals(autoId, ft.isAutoID());
  2272. assertEquals(primaryKey, ft.isPrimaryKey());
  2273. assertEquals(dim, ft.getDimension());
  2274. assertFalse(wrapper.toString().isEmpty());
  2275. }
  2276. @Test
  2277. void testDescIndexResponseWrapper() {
  2278. final long indexId = 888;
  2279. String indexName = "idx";
  2280. String fieldName = "f1";
  2281. IndexType indexType = IndexType.IVF_FLAT;
  2282. MetricType metricType = MetricType.IP;
  2283. String extraParam = "{nlist:10}";
  2284. KeyValuePair kvIndexType = KeyValuePair.newBuilder()
  2285. .setKey(Constant.INDEX_TYPE).setValue(indexType.name()).build();
  2286. KeyValuePair kvMetricType = KeyValuePair.newBuilder()
  2287. .setKey(Constant.METRIC_TYPE).setValue(metricType.name()).build();
  2288. KeyValuePair kvExtraParam = KeyValuePair.newBuilder()
  2289. .setKey(Constant.PARAMS).setValue(extraParam).build();
  2290. IndexDescription desc = IndexDescription.newBuilder()
  2291. .setIndexID(indexId)
  2292. .setIndexName(indexName)
  2293. .setFieldName(fieldName)
  2294. .addParams(kvIndexType)
  2295. .addParams(kvMetricType)
  2296. .addParams(kvExtraParam)
  2297. .build();
  2298. DescribeIndexResponse response = DescribeIndexResponse.newBuilder()
  2299. .addIndexDescriptions(desc)
  2300. .build();
  2301. DescIndexResponseWrapper wrapper = new DescIndexResponseWrapper(response);
  2302. assertEquals(1, wrapper.getIndexDescriptions().size());
  2303. assertNull(wrapper.getIndexDescByFieldName(""));
  2304. DescIndexResponseWrapper.IndexDesc indexDesc = wrapper.getIndexDescByFieldName(fieldName);
  2305. assertEquals(indexId, indexDesc.getId());
  2306. assertEquals(indexName, indexDesc.getIndexName());
  2307. assertEquals(fieldName, indexDesc.getFieldName());
  2308. assertEquals(indexType, indexDesc.getIndexType());
  2309. assertEquals(metricType, indexDesc.getMetricType());
  2310. assertEquals(0, extraParam.compareTo(indexDesc.getExtraParam()));
  2311. assertFalse(wrapper.toString().isEmpty());
  2312. }
  2313. @SuppressWarnings("unchecked")
  2314. @Test
  2315. void testFieldDataWrapper() {
  2316. // for float vector
  2317. long dim = 3;
  2318. List<Float> floatVectors = Arrays.asList(1F, 2F, 3F, 4F, 5F, 6F);
  2319. FieldData fieldData = FieldData.newBuilder()
  2320. .setFieldName("vec")
  2321. .setFieldId(1L)
  2322. .setType(DataType.FloatVector)
  2323. .setVectors(VectorField.newBuilder()
  2324. .setDim(dim)
  2325. .setFloatVector(FloatArray.newBuilder()
  2326. .addAllData(floatVectors)
  2327. .build())
  2328. .build())
  2329. .build();
  2330. FieldDataWrapper wrapper = new FieldDataWrapper(fieldData);
  2331. assertEquals(dim, wrapper.getDim());
  2332. assertEquals(floatVectors.size() / dim, wrapper.getRowCount());
  2333. List<?> floatData = wrapper.getFieldData();
  2334. assertEquals(floatVectors.size() / dim, floatData.size());
  2335. for (Object obj : floatData) {
  2336. List<Float> vec = (List<Float>) obj;
  2337. assertEquals(dim, vec.size());
  2338. }
  2339. // for binary vector
  2340. dim = 16;
  2341. int bytesPerVec = (int) (dim/8);
  2342. int count = 2;
  2343. byte[] binary = new byte[bytesPerVec * count];
  2344. for (int i = 0; i < binary.length; ++i) {
  2345. binary[i] = (byte) i;
  2346. }
  2347. fieldData = FieldData.newBuilder()
  2348. .setFieldName("vec")
  2349. .setFieldId(1L)
  2350. .setType(DataType.BinaryVector)
  2351. .setVectors(VectorField.newBuilder()
  2352. .setDim(dim)
  2353. .setBinaryVector(ByteString.copyFrom(binary))
  2354. .build())
  2355. .build();
  2356. wrapper = new FieldDataWrapper(fieldData);
  2357. assertEquals(dim, wrapper.getDim());
  2358. assertEquals(count, wrapper.getRowCount());
  2359. List<?> binaryData = wrapper.getFieldData();
  2360. assertEquals(count, binaryData.size());
  2361. for(int i = 0; i < binaryData.size(); i++) {
  2362. ByteBuffer vec = (ByteBuffer) binaryData.get(i);
  2363. assertEquals(bytesPerVec, vec.limit());
  2364. for(int j = 0; j < bytesPerVec; j++) {
  2365. assertEquals(binary[i*bytesPerVec + j], vec.get(j));
  2366. }
  2367. }
  2368. // for scalar field
  2369. LongArray.Builder int64Builder = LongArray.newBuilder();
  2370. for (long i = 0; i < dim; ++i) {
  2371. int64Builder.addData(i);
  2372. }
  2373. testScalarField(ScalarField.newBuilder().setLongData(int64Builder).build(),
  2374. DataType.Int64, dim);
  2375. IntArray.Builder intBuilder = IntArray.newBuilder();
  2376. for (int i = 0; i < dim; ++i) {
  2377. intBuilder.addData(i);
  2378. }
  2379. testScalarField(ScalarField.newBuilder().setIntData(intBuilder).build(),
  2380. DataType.Int32, dim);
  2381. testScalarField(ScalarField.newBuilder().setIntData(intBuilder).build(),
  2382. DataType.Int16, dim);
  2383. testScalarField(ScalarField.newBuilder().setIntData(intBuilder).build(),
  2384. DataType.Int8, dim);
  2385. BoolArray.Builder boolBuilder = BoolArray.newBuilder();
  2386. for (long i = 0; i < dim; ++i) {
  2387. boolBuilder.addData(i % 2 == 0);
  2388. }
  2389. testScalarField(ScalarField.newBuilder().setBoolData(boolBuilder).build(),
  2390. DataType.Bool, dim);
  2391. FloatArray.Builder floatBuilder = FloatArray.newBuilder();
  2392. for (long i = 0; i < dim; ++i) {
  2393. floatBuilder.addData((float) i);
  2394. }
  2395. testScalarField(ScalarField.newBuilder().setFloatData(floatBuilder).build(),
  2396. DataType.Float, dim);
  2397. DoubleArray.Builder doubleBuilder = DoubleArray.newBuilder();
  2398. for (long i = 0; i < dim; ++i) {
  2399. doubleBuilder.addData((double) i);
  2400. }
  2401. testScalarField(ScalarField.newBuilder().setDoubleData(doubleBuilder).build(),
  2402. DataType.Double, dim);
  2403. StringArray.Builder strBuilder = StringArray.newBuilder();
  2404. for (long i = 0; i < dim; ++i) {
  2405. strBuilder.addData(String.valueOf(i));
  2406. }
  2407. testScalarField(ScalarField.newBuilder().setStringData(strBuilder).build(),
  2408. DataType.VarChar, dim);
  2409. }
  2410. @Test
  2411. void testGetCollStatResponseWrapper() {
  2412. GetCollectionStatisticsResponse response = GetCollectionStatisticsResponse.newBuilder()
  2413. .addStats(KeyValuePair.newBuilder().setKey(Constant.ROW_COUNT).setValue("invalid").build())
  2414. .build();
  2415. GetCollStatResponseWrapper invalidWrapper = new GetCollStatResponseWrapper(response);
  2416. assertThrows(NumberFormatException.class, invalidWrapper::getRowCount);
  2417. response = GetCollectionStatisticsResponse.newBuilder()
  2418. .addStats(KeyValuePair.newBuilder().setKey(Constant.ROW_COUNT).setValue("10").build())
  2419. .build();
  2420. GetCollStatResponseWrapper wrapper = new GetCollStatResponseWrapper(response);
  2421. assertEquals(10, wrapper.getRowCount());
  2422. response = GetCollectionStatisticsResponse.newBuilder().build();
  2423. wrapper = new GetCollStatResponseWrapper(response);
  2424. assertEquals(0, wrapper.getRowCount());
  2425. }
  2426. @Test
  2427. void testGetPartStatResponseWrapper() {
  2428. final long rowCount = 500;
  2429. KeyValuePair kvStat = KeyValuePair.newBuilder()
  2430. .setKey(Constant.ROW_COUNT).setValue(String.valueOf(rowCount)).build();
  2431. GetPartitionStatisticsResponse response = GetPartitionStatisticsResponse.newBuilder()
  2432. .addStats(kvStat).build();
  2433. GetPartStatResponseWrapper wrapper = new GetPartStatResponseWrapper(response);
  2434. assertEquals(rowCount, wrapper.getRowCount());
  2435. response = GetPartitionStatisticsResponse.newBuilder().build();
  2436. wrapper = new GetPartStatResponseWrapper(response);
  2437. assertEquals(0, wrapper.getRowCount());
  2438. }
  2439. @Test
  2440. void testMutationResultWrapper() {
  2441. List<Long> nID = Arrays.asList(1L, 2L, 3L);
  2442. MutationResult results = MutationResult.newBuilder()
  2443. .setInsertCnt(nID.size())
  2444. .setDeleteCnt(nID.size())
  2445. .setIDs(IDs.newBuilder()
  2446. .setIntId(LongArray.newBuilder()
  2447. .addAllData(nID)
  2448. .build()))
  2449. .setTimestamp(1000)
  2450. .build();
  2451. MutationResultWrapper longWrapper = new MutationResultWrapper(results);
  2452. assertEquals(1000, longWrapper.getOperationTs());
  2453. assertEquals(nID.size(), longWrapper.getInsertCount());
  2454. assertEquals(nID.size(), longWrapper.getDeleteCount());
  2455. assertThrows(ParamException.class, longWrapper::getStringIDs);
  2456. List<Long> longIDs = longWrapper.getLongIDs();
  2457. assertEquals(nID.size(), longIDs.size());
  2458. for (int i = 0; i < longIDs.size(); ++i) {
  2459. assertEquals(nID.get(i), longIDs.get(i));
  2460. }
  2461. List<String> sID = Arrays.asList("1", "2", "3");
  2462. results = MutationResult.newBuilder()
  2463. .setInsertCnt(sID.size())
  2464. .setIDs(IDs.newBuilder()
  2465. .setStrId(StringArray.newBuilder()
  2466. .addAllData(sID)
  2467. .build()))
  2468. .build();
  2469. MutationResultWrapper strWrapper = new MutationResultWrapper(results);
  2470. assertEquals(sID.size(), strWrapper.getInsertCount());
  2471. assertThrows(ParamException.class, strWrapper::getLongIDs);
  2472. List<String> strIDs = strWrapper.getStringIDs();
  2473. assertEquals(sID.size(), strIDs.size());
  2474. for (int i = 0; i < strIDs.size(); ++i) {
  2475. assertEquals(sID.get(i), strIDs.get(i));
  2476. }
  2477. }
  2478. @Test
  2479. void testQueryResultsWrapper() {
  2480. String fieldName = "test";
  2481. QueryResults results = QueryResults.newBuilder()
  2482. .addFieldsData(FieldData.newBuilder()
  2483. .setFieldName(fieldName)
  2484. .build())
  2485. .build();
  2486. QueryResultsWrapper wrapper = new QueryResultsWrapper(results);
  2487. assertThrows(ParamException.class, () -> wrapper.getFieldWrapper("invalid"));
  2488. assertNotNull(wrapper.getFieldWrapper(fieldName));
  2489. }
  2490. @Test
  2491. void testSearchResultsWrapper() {
  2492. long topK = 5;
  2493. long numQueries = 2;
  2494. List<Long> longIDs = new ArrayList<>();
  2495. List<String> strIDs = new ArrayList<>();
  2496. List<Float> scores = new ArrayList<>();
  2497. List<Double> outputField = new ArrayList<>();
  2498. for (long i = 0; i < topK * numQueries; ++i) {
  2499. longIDs.add(i);
  2500. strIDs.add(String.valueOf(i));
  2501. scores.add((float) i);
  2502. outputField.add((double) i);
  2503. }
  2504. // for long id
  2505. DoubleArray.Builder doubleArrayBuilder = DoubleArray.newBuilder();
  2506. outputField.forEach(doubleArrayBuilder::addData);
  2507. String fieldName = "test";
  2508. SearchResultData results = SearchResultData.newBuilder()
  2509. .setTopK(topK)
  2510. .setNumQueries(numQueries)
  2511. .setIds(IDs.newBuilder()
  2512. .setIntId(LongArray.newBuilder()
  2513. .addAllData(longIDs)
  2514. .build()))
  2515. .addAllScores(scores)
  2516. .addFieldsData(FieldData.newBuilder()
  2517. .setFieldName(fieldName)
  2518. .setType(DataType.Double)
  2519. .setScalars(ScalarField.newBuilder()
  2520. .setDoubleData(doubleArrayBuilder.build())
  2521. .build()))
  2522. .build();
  2523. SearchResultsWrapper intWrapper = new SearchResultsWrapper(results);
  2524. assertThrows(ParamException.class, () -> intWrapper.getFieldData(fieldName, -1));
  2525. assertThrows(ParamException.class, () -> intWrapper.getFieldData("invalid", 0));
  2526. assertEquals(topK, intWrapper.getFieldData(fieldName, (int) numQueries - 1).size());
  2527. List<SearchResultsWrapper.IDScore> idScores = intWrapper.getIDScore(1);
  2528. assertFalse(idScores.toString().isEmpty());
  2529. assertEquals(topK, idScores.size());
  2530. assertThrows(ParamException.class, () -> intWrapper.getIDScore((int) numQueries));
  2531. // for string id
  2532. results = SearchResultData.newBuilder()
  2533. .setTopK(topK)
  2534. .setNumQueries(numQueries)
  2535. .setIds(IDs.newBuilder()
  2536. .setStrId(StringArray.newBuilder()
  2537. .addAllData(strIDs)
  2538. .build()))
  2539. .addAllScores(scores)
  2540. .addFieldsData(FieldData.newBuilder()
  2541. .setFieldName(fieldName)
  2542. .build())
  2543. .build();
  2544. SearchResultsWrapper strWrapper = new SearchResultsWrapper(results);
  2545. idScores = strWrapper.getIDScore(0);
  2546. assertFalse(idScores.toString().isEmpty());
  2547. assertEquals(topK, idScores.size());
  2548. idScores.forEach((score) -> assertFalse(score.toString().isEmpty()));
  2549. }
  2550. @Test
  2551. void testShowCollResponseWrapper() {
  2552. List<String> names = Arrays.asList("coll_1", "coll_2");
  2553. List<Long> ids = Arrays.asList(1L, 2L);
  2554. List<Long> ts = Arrays.asList(888L, 999L);
  2555. List<Long> inMemory = Arrays.asList(100L, 50L);
  2556. ShowCollectionsResponse response = ShowCollectionsResponse.newBuilder()
  2557. .addAllCollectionNames(names)
  2558. .addAllCollectionIds(ids)
  2559. .addAllCreatedUtcTimestamps(ts)
  2560. .addAllInMemoryPercentages(inMemory)
  2561. .build();
  2562. ShowCollResponseWrapper wrapper = new ShowCollResponseWrapper(response);
  2563. assertEquals(names.size(), wrapper.getCollectionsInfo().size());
  2564. assertFalse(wrapper.toString().isEmpty());
  2565. for (int i = 0; i < 2; ++i) {
  2566. ShowCollResponseWrapper.CollectionInfo info = wrapper.getCollectionInfoByName(names.get(i));
  2567. assertEquals(0, names.get(i).compareTo(info.getName()));
  2568. assertEquals(ids.get(i), info.getId());
  2569. assertEquals(ts.get(i), info.getUtcTimestamp());
  2570. assertEquals(inMemory.get(i), info.getInMemoryPercentage());
  2571. assertFalse(info.toString().isEmpty());
  2572. }
  2573. }
  2574. @Test
  2575. void testShowPartResponseWrapper() {
  2576. List<String> names = Arrays.asList("part_1", "part_2");
  2577. List<Long> ids = Arrays.asList(1L, 2L);
  2578. List<Long> ts = Arrays.asList(888L, 999L);
  2579. List<Long> inMemory = Arrays.asList(100L, 50L);
  2580. ShowPartitionsResponse response = ShowPartitionsResponse.newBuilder()
  2581. .addAllPartitionNames(names)
  2582. .addAllPartitionIDs(ids)
  2583. .addAllCreatedUtcTimestamps(ts)
  2584. .addAllInMemoryPercentages(inMemory)
  2585. .build();
  2586. ShowPartResponseWrapper wrapper = new ShowPartResponseWrapper(response);
  2587. assertEquals(names.size(), wrapper.getPartitionsInfo().size());
  2588. assertFalse(wrapper.toString().isEmpty());
  2589. for (int i = 0; i < 2; ++i) {
  2590. ShowPartResponseWrapper.PartitionInfo info = wrapper.getPartitionInfoByName(names.get(i));
  2591. assertEquals(0, names.get(i).compareTo(info.getName()));
  2592. assertEquals(ids.get(i), info.getId());
  2593. assertEquals(ts.get(i), info.getUtcTimestamp());
  2594. assertEquals(inMemory.get(i), info.getInMemoryPercentage());
  2595. assertFalse(info.toString().isEmpty());
  2596. }
  2597. }
  2598. @Test
  2599. void testGetBulkInsertStateWrapper() {
  2600. long count = 1000;
  2601. ImportState state = ImportState.ImportStarted;
  2602. String reason = "unexpected error";
  2603. String files = "1.json";
  2604. String collection = "c1";
  2605. String partition = "p1";
  2606. String progress = "50";
  2607. GetImportStateResponse resp = GetImportStateResponse.newBuilder()
  2608. .setState(state)
  2609. .setRowCount(count)
  2610. .addIdList(0)
  2611. .addIdList(99)
  2612. .addInfos(KeyValuePair.newBuilder()
  2613. .setKey(Constant.FAILED_REASON)
  2614. .setValue(reason)
  2615. .build())
  2616. .addInfos(KeyValuePair.newBuilder()
  2617. .setKey(Constant.IMPORT_FILES)
  2618. .setValue(files)
  2619. .build())
  2620. .addInfos(KeyValuePair.newBuilder()
  2621. .setKey(Constant.IMPORT_COLLECTION)
  2622. .setValue(collection)
  2623. .build())
  2624. .addInfos(KeyValuePair.newBuilder()
  2625. .setKey(Constant.IMPORT_PARTITION)
  2626. .setValue(partition)
  2627. .build())
  2628. .addInfos(KeyValuePair.newBuilder()
  2629. .setKey(Constant.IMPORT_PROGRESS)
  2630. .setValue(progress)
  2631. .build())
  2632. .build();
  2633. GetBulkInsertStateWrapper wrapper = new GetBulkInsertStateWrapper(resp);
  2634. assertEquals(count, wrapper.getImportedCount());
  2635. assertEquals(100, wrapper.getAutoGeneratedIDs().size());
  2636. assertEquals(0, wrapper.getAutoGeneratedIDs().get(0));
  2637. assertEquals(99, wrapper.getAutoGeneratedIDs().get(99));
  2638. assertEquals(reason, wrapper.getFailedReason());
  2639. assertEquals(files, wrapper.getFiles());
  2640. assertEquals(collection, wrapper.getCollectionName());
  2641. assertEquals(partition, wrapper.getPartitionName());
  2642. assertEquals(progress, String.valueOf(wrapper.getProgress()));
  2643. assertFalse(wrapper.toString().isEmpty());
  2644. }
  2645. }