Browse Source

Fix the problem of calculating the total size of files that are backed up to the object storage service.

Shentar 1 year ago
parent
commit
85f2a1a9bd

+ 14 - 12
photoweb/src/main/java/com/backend/dao/BackupedFilesDao.java

@@ -16,19 +16,19 @@ public abstract class BackupedFilesDao extends AbstractRecordsStore
 
     protected abstract String getBackupedTableName();
 
-    public void checkAndaddOneRecords(String hashStr, String eTag, String objkey)
+    public boolean checkAndaddOneRecords(String hashStr, String eTag, String objkey)
     {
         if (StringUtils.isBlank(hashStr))
         {
             logger.warn("the input hashstr is blank.");
-            return;
+            return false;
         }
 
-        PreparedStatement prep;
-        ResultSet res;
+        PreparedStatement prep = null;
+        ResultSet res = null;
+        lock.writeLock().lock();
         try
         {
-            lock.readLock().lock();
             prep = conn.prepareStatement(
                     "select * from " + getBackupedTableName() + " where hashstr=?;");
             prep.setString(1, hashStr);
@@ -37,26 +37,28 @@ public abstract class BackupedFilesDao extends AbstractRecordsStore
             if (res.next())
             {
                 logger.warn("the file is already backuped: [{}:{}:{}]", hashStr, eTag, objkey);
-                closeResource(prep, res);
-                lock.readLock().unlock();
-                return;
+                return false;
             }
-            lock.readLock().unlock();
             closeResource(prep, res);
+            res = null;
 
-            lock.writeLock().lock();
             prep = conn.prepareStatement(
                     "insert into " + getBackupedTableName() + " values(?,?,?);");
             prep.setString(1, hashStr);
             prep.setString(2, eTag);
             prep.setString(3, objkey);
             prep.execute();
-            lock.writeLock().unlock();
-            closeResource(prep, res);
+            return true;
         }
         catch (SQLException e)
         {
             logger.error("caught: " + hashStr, e);
+            return false;
+        }
+        finally
+        {
+            closeResource(prep, res);
+            lock.writeLock().unlock();
         }
     }
 

+ 0 - 1
photoweb/src/main/java/com/backend/dao/BaseSqliteStore.java

@@ -504,7 +504,6 @@ public class BaseSqliteStore extends AbstractRecordsStore
         PreparedStatement prep = null;
         try
         {
-            Thread.sleep(1500);
             lock.writeLock().lock();
             if (isPath)
             {

+ 11 - 5
photoweb/src/main/java/com/backend/sync/s3/AbstractSyncS3Service.java

@@ -101,10 +101,13 @@ public abstract class AbstractSyncS3Service
             {
                 // 使用jalbum提取的指纹计算得到的etag来做key,而不是使用真实的文件的etag。
                 allobjs.put(fi.getHash256().toUpperCase(), o);
-                backedUpSize.addAndGet(o.getContentLength());
-                getBackupedFileDao()
+                // 注意此处,有可能两个线程同时上传同一张照片,但是文件不同。此时可能导致总存量计算不正确。
+                if (getBackupedFileDao()
                         .checkAndaddOneRecords(fi.getHash256().toUpperCase(), o.getETag(),
-                                               o.getKey());
+                                               o.getKey()))
+                {
+                    backedUpSize.addAndGet(o.getContentLength());
+                }
             }
             else
             {
@@ -214,8 +217,11 @@ public abstract class AbstractSyncS3Service
                     }
 
                     logger.warn("the file is in s3 but not in the local table: {}", o);
-                    getBackupedFileDao().checkAndaddOneRecords(hashStr, o.getETag(), o.getKey());
-                    allobjs.put(hashStr, o);
+                    if (getBackupedFileDao()
+                            .checkAndaddOneRecords(hashStr, o.getETag(), o.getKey()))
+                    {
+                        allobjs.put(hashStr, o);
+                    }
                 }
 
                 if (sc.isListingComplete())

+ 22 - 13
photoweb/src/main/java/com/service/ObjectService.java

@@ -324,7 +324,7 @@ public class ObjectService
 
     @DELETE
     public Response deletePhotoData(@Context HttpServletRequest req,
-            @Context HttpServletResponse response) throws IOException
+                                    @Context HttpServletResponse response) throws IOException
     {
 
         logger.warn("try to delete the photo: " + id);
@@ -337,18 +337,27 @@ public class ObjectService
         }
         else
         {
-            BaseSqliteStore.getInstance().setPhotoToBeHiden(id, false);
-            UniqPhotosStore.getInstance().deleteRecordByID(id);
-            FaceTableDao.getInstance().deleteOneFile(id);
-
-            /*
-             * if (fnext!= null && !fnext.isEmpty()) { // 刷新整个页面。 String
-             * bodyContent = GenerateHTML.generateSinglePhoto(fnext.get(0));
-             * builder.header("Content-type", "text/html");
-             * builder.entity(bodyContent); logger.info("the page is {}",
-             * bodyContent); }
-             */
-            logger.warn("deleted the photo: " + id);
+            try
+            {
+                // 延迟1.5s下发删除请求,前端刷新页面时需要依赖此条记录。盲等前端刷新玩页面后再行删除。
+                Thread.sleep(1500);
+                BaseSqliteStore.getInstance().setPhotoToBeHiden(id, false);
+                UniqPhotosStore.getInstance().deleteRecordByID(id);
+                FaceTableDao.getInstance().deleteOneFile(id);
+
+                /*
+                 * if (fnext!= null && !fnext.isEmpty()) { // 刷新整个页面。 String
+                 * bodyContent = GenerateHTML.generateSinglePhoto(fnext.get(0));
+                 * builder.header("Content-type", "text/html");
+                 * builder.entity(bodyContent); logger.info("the page is {}",
+                 * bodyContent); }
+                 */
+                logger.warn("deleted the photo: " + id);
+            }
+            catch (Exception e)
+            {
+                logger.warn("caught by, ", e);
+            }
         }
 
         return builder.build();