/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.master.block;

import alluxio.AlluxioURI;
import alluxio.LocalAlluxioClusterResource;
import alluxio.PropertyKey;
import alluxio.client.WriteType;
import alluxio.client.file.FileSystem;
import alluxio.client.file.FileSystemTestUtils;
import alluxio.master.LocalAlluxioCluster;
import alluxio.master.file.FileSystemMaster;
import alluxio.master.file.meta.InodeTree;
import alluxio.master.file.meta.LockedInodePath;
import alluxio.master.file.options.DeleteOptions;
import alluxio.master.journal.JournalContext;
import alluxio.util.CommonUtils;
import alluxio.util.WaitForOptions;
import alluxio.worker.block.BlockWorker;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

public class BlockMasterIntegrityIntegrationTest {
  @Rule
  public LocalAlluxioClusterResource mClusterResource =
      new LocalAlluxioClusterResource.Builder().build();
  private LocalAlluxioCluster mCluster;

  @Before
  public void before() {
    mCluster = mClusterResource.get();
  }

  @Test
  public void deleteOrphanedBlocks() throws Exception {
    AlluxioURI uri = new AlluxioURI("/test");
    int len = 10;
    FileSystem fs = mCluster.getClient();
    BlockWorker worker = mCluster.getWorkerProcess().getWorker(BlockWorker.class);
    FileSystemTestUtils.createByteFile(fs, uri, WriteType.MUST_CACHE, len);
    Assert.assertEquals(1, worker.getStoreMetaFull().getNumberOfBlocks());
    mCluster.stopWorkers();
    fs.delete(uri);
    mCluster.restartMasters();
    mCluster.startWorkers(); // creates a new worker, so need to get the new BlockWorker
    BlockWorker newWorker = mCluster.getWorkerProcess().getWorker(BlockWorker.class);
    CommonUtils.waitFor("orphan blocks to be deleted",
        (v) -> newWorker.getStoreMetaFull().getNumberOfBlocks() == 0,
        WaitForOptions.defaults().setTimeoutMs(2000));
  }

  @Test
  @LocalAlluxioClusterResource.Config(confParams = {
      PropertyKey.Name.MASTER_STARTUP_BLOCK_INTEGRITY_CHECK_ENABLED, "true"
      })
  public void deleteInvalidBlocks() throws Exception {
    AlluxioURI uri = new AlluxioURI("/test");
    int len = 10;
    FileSystem fs = mCluster.getClient();
    BlockWorker worker = mCluster.getWorkerProcess().getWorker(BlockWorker.class);
    FileSystemTestUtils.createByteFile(fs, uri, WriteType.MUST_CACHE, len);
    Assert.assertEquals(1, worker.getStoreMetaFull().getNumberOfBlocks());
    FileSystemMaster fsm =
        mCluster.getLocalAlluxioMaster().getMasterProcess().getMaster(FileSystemMaster.class);
    InodeTree tree = Whitebox.getInternalState(fsm, "mInodeTree");
    LockedInodePath path = tree.lockInodePath(uri, InodeTree.LockMode.WRITE);
    DeleteOptions options = DeleteOptions.defaults();
    JournalContext ctx = Whitebox.invokeMethod(fsm, "createJournalContext");
    Whitebox.invokeMethod(fsm, "deleteAndJournal", path, options, ctx);
    ctx.close();
    mCluster.stopWorkers();
    mCluster.restartMasters();
    mCluster.startWorkers(); // creates a new worker, so need to get the new BlockWorker
    BlockWorker newWorker = mCluster.getWorkerProcess().getWorker(BlockWorker.class);
    CommonUtils.waitFor("invalid blocks to be deleted",
        (v) -> newWorker.getStoreMetaFull().getNumberOfBlocks() == 0,
        WaitForOptions.defaults().setTimeoutMs(2000));
  }

  @Test
  @LocalAlluxioClusterResource.Config(confParams = {
      PropertyKey.Name.MASTER_PERIODIC_BLOCK_INTEGRITY_CHECK_INTERVAL, "1sec",
      PropertyKey.Name.MASTER_PERIODIC_BLOCK_INTEGRITY_CHECK_REPAIR, "true"
      })
  public void deleteInvalidBlocksPeriodically() throws Exception {
    AlluxioURI uri = new AlluxioURI("/test");
    int len = 10;
    FileSystem fs = mCluster.getClient();
    BlockWorker worker = mCluster.getWorkerProcess().getWorker(BlockWorker.class);
    FileSystemTestUtils.createByteFile(fs, uri, WriteType.MUST_CACHE, len);
    Assert.assertEquals(1, worker.getStoreMetaFull().getNumberOfBlocks());
    FileSystemMaster fsm =
        mCluster.getLocalAlluxioMaster().getMasterProcess().getMaster(FileSystemMaster.class);
    InodeTree tree = Whitebox.getInternalState(fsm, "mInodeTree");
    LockedInodePath path = tree.lockInodePath(uri, InodeTree.LockMode.WRITE);
    DeleteOptions options = DeleteOptions.defaults();
    JournalContext ctx = Whitebox.invokeMethod(fsm, "createJournalContext");
    Whitebox.invokeMethod(fsm, "deleteAndJournal", path, options, ctx);
    ctx.close();
    CommonUtils.waitFor("invalid blocks to be deleted",
        (v) -> worker.getStoreMetaFull().getNumberOfBlocks() == 0,
        WaitForOptions.defaults().setTimeoutMs(2000));
  }
}

@@ -796,24 +796,73 @@
 xioCluster;%0A
+import alluxio.master.file.BlockDeletionContext;%0A
 import allux
@@ -3318,32 +3318,122 @@
 ons.defaults();%0A
+    BlockDeletionContext bctx = Whitebox.invokeMethod(fsm, %22createBlockDeletionContext%22);%0A
     JournalConte
@@ -3427,32 +3427,33 @@
  JournalContext 
+j
 ctx = Whitebox.i
@@ -3552,32 +3552,39 @@
  path, options, 
+jctx, b
 ctx);%0A    ctx.cl
@@ -3569,32 +3569,119 @@
 ctx, bctx);%0A    
+path.close();%0A    jctx.close(); // Journal Context is closed before Block Context%0A    b
 ctx.close();%0A   
@@ -5046,24 +5046,114 @@
 defaults();%0A
+    BlockDeletionContext bctx = Whitebox.invokeMethod(fsm, %22createBlockDeletionContext%22);%0A
     JournalC
@@ -5159,16 +5159,17 @@
 Context 
+j
 ctx = Wh
@@ -5284,16 +5284,23 @@
 ptions, 
+jctx, b
 ctx);%0A  
@@ -5301,16 +5301,103 @@
 x);%0A    
+path.close();%0A    jctx.close(); // Journal Context is closed before Block Context%0A    b
 ctx.clos

Update Block Integrity Check Test. (#6991)