2323import org .apache .iotdb .common .rpc .thrift .TSStatus ;
2424import org .apache .iotdb .commons .exception .IoTDBRuntimeException ;
2525import org .apache .iotdb .commons .executable .ExecutableManager ;
26+ import org .apache .iotdb .commons .executable .ReferenceCountedJarMetaKeeper ;
2627import org .apache .iotdb .commons .snapshot .SnapshotProcessor ;
2728import org .apache .iotdb .commons .udf .UDFInformation ;
2829import org .apache .iotdb .commons .udf .UDFTable ;
4142import org .apache .iotdb .rpc .TSStatusCode ;
4243import org .apache .iotdb .udf .api .exception .UDFManagementException ;
4344
44- import org .apache .tsfile .utils .ReadWriteIOUtils ;
4545import org .slf4j .Logger ;
4646import org .slf4j .LoggerFactory ;
4747
5454import java .nio .ByteBuffer ;
5555import java .util .ArrayList ;
5656import java .util .Collections ;
57- import java .util .HashMap ;
5857import java .util .List ;
5958import java .util .Map ;
6059import java .util .concurrent .locks .ReentrantLock ;
@@ -67,7 +66,7 @@ public class UDFInfo implements SnapshotProcessor {
6766 ConfigNodeDescriptor .getInstance ().getConf ();
6867
6968 private final UDFTable udfTable ;
70- private final Map < String , String > existedJarToMD5 ;
69+ private final ReferenceCountedJarMetaKeeper jarMetaKeeper ;
7170
7271 private final UDFExecutableManager udfExecutableManager ;
7372
@@ -77,7 +76,7 @@ public class UDFInfo implements SnapshotProcessor {
7776
7877 public UDFInfo () throws IOException {
7978 udfTable = new UDFTable ();
80- existedJarToMD5 = new HashMap <> ();
79+ jarMetaKeeper = new ReferenceCountedJarMetaKeeper ();
8180 udfExecutableManager =
8281 UDFExecutableManager .setupAndGetInstance (
8382 CONFIG_NODE_CONF .getUdfTemporaryLibDir (), CONFIG_NODE_CONF .getUdfDir ());
@@ -104,7 +103,8 @@ public void validate(Model model, String udfName, String jarName, String jarMD5)
104103 TSStatusCode .UDF_ALREADY_EXISTS .getStatusCode ());
105104 }
106105
107- if (existedJarToMD5 .containsKey (jarName ) && !existedJarToMD5 .get (jarName ).equals (jarMD5 )) {
106+ if (jarMetaKeeper .containsJar (jarName )
107+ && !jarMetaKeeper .jarNameExistsAndMatchesMd5 (jarName , jarMD5 )) {
108108 throw new IoTDBRuntimeException (
109109 String .format (
110110 ConfigNodeMessages .FAILED_TO_CREATE_UDF_THE_SAME_NAME_JAR_BUT_DIFFERENT ,
@@ -127,15 +127,15 @@ public UDFInformation getUDFInformation(Model model, String udfName)
127127 }
128128
129129 public boolean needToSaveJar (String jarName ) {
130- return ! existedJarToMD5 . containsKey (jarName );
130+ return jarMetaKeeper . needToSaveJar (jarName );
131131 }
132132
133133 public TSStatus addUDFInTable (CreateFunctionPlan physicalPlan ) {
134134 try {
135135 final UDFInformation udfInformation = physicalPlan .getUdfInformation ();
136136 udfTable .addUDFInformation (udfInformation .getFunctionName (), udfInformation );
137137 if (udfInformation .isUsingURI ()) {
138- existedJarToMD5 . put (udfInformation .getJarName (), udfInformation .getJarMD5 ());
138+ addJarReference (udfInformation .getJarName (), udfInformation .getJarMD5 ());
139139 if (physicalPlan .getJarFile () != null ) {
140140 udfExecutableManager .saveToInstallDir (
141141 ByteBuffer .wrap (physicalPlan .getJarFile ().getValues ()), udfInformation .getJarName ());
@@ -185,7 +185,10 @@ public JarResp getUDFJar(GetUDFJarPlan physicalPlan) {
185185
186186 public TSStatus dropFunction (Model model , String functionName ) {
187187 if (udfTable .containsUDF (model , functionName )) {
188- existedJarToMD5 .remove (udfTable .getUDFInformation (model , functionName ).getJarName ());
188+ final UDFInformation udfInformation = udfTable .getUDFInformation (model , functionName );
189+ if (udfInformation .isUsingURI ()) {
190+ removeJarReference (udfInformation .getJarName ());
191+ }
189192 udfTable .removeUDFInformation (model , functionName );
190193 }
191194 return new TSStatus (TSStatusCode .SUCCESS_STATUS .getStatusCode ());
@@ -204,7 +207,7 @@ public Map<Model, Map<String, UDFInformation>> getRawUDFTable() {
204207
205208 @ TestOnly
206209 public Map <String , String > getRawExistedJarToMD5 () {
207- return existedJarToMD5 ;
210+ return jarMetaKeeper . getJarNameToMd5Map () ;
208211 }
209212
210213 @ Override
@@ -248,30 +251,39 @@ public void processLoadSnapshot(File snapshotDir) throws IOException {
248251 deserializeExistedJarToMD5 (fileInputStream );
249252
250253 udfTable .deserializeUDFTable (fileInputStream );
254+ rebuildJarMetadataFromUDFTable ();
251255 } finally {
252256 releaseUDFTableLock ();
253257 }
254258 }
255259
256260 public void serializeExistedJarToMD5 (OutputStream outputStream ) throws IOException {
257- ReadWriteIOUtils .write (existedJarToMD5 .size (), outputStream );
258- for (Map .Entry <String , String > entry : existedJarToMD5 .entrySet ()) {
259- ReadWriteIOUtils .write (entry .getKey (), outputStream );
260- ReadWriteIOUtils .write (entry .getValue (), outputStream );
261- }
261+ jarMetaKeeper .serializeJarNameToMd5 (outputStream );
262262 }
263263
264264 public void deserializeExistedJarToMD5 (InputStream inputStream ) throws IOException {
265- int size = ReadWriteIOUtils .readInt (inputStream );
266- while (size > 0 ) {
267- existedJarToMD5 .put (
268- ReadWriteIOUtils .readString (inputStream ), ReadWriteIOUtils .readString (inputStream ));
269- size --;
270- }
265+ jarMetaKeeper .deserializeJarNameToMd5 (inputStream );
271266 }
272267
273268 public void clear () {
274- existedJarToMD5 .clear ();
269+ jarMetaKeeper .clear ();
275270 udfTable .clear ();
276271 }
272+
273+ private void addJarReference (String jarName , String jarMD5 ) {
274+ jarMetaKeeper .addReference (jarName , jarMD5 );
275+ }
276+
277+ private void removeJarReference (String jarName ) {
278+ jarMetaKeeper .removeReference (jarName );
279+ }
280+
281+ private void rebuildJarMetadataFromUDFTable () {
282+ jarMetaKeeper .clear ();
283+ for (UDFInformation udfInformation : udfTable .getAllInformationList ()) {
284+ if (udfInformation .isUsingURI ()) {
285+ addJarReference (udfInformation .getJarName (), udfInformation .getJarMD5 ());
286+ }
287+ }
288+ }
277289}
0 commit comments