Skip to content

Commit

Permalink
fixed issue with meta lock not getting deleted for rename table
Browse files Browse the repository at this point in the history
  • Loading branch information
kunal642 authored and ravipesala committed Apr 6, 2017
1 parent 9554971 commit 7ae1cd8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.carbondata.core.locks;

import org.apache.carbondata.core.constants.CarbonCommonConstants;
import org.apache.carbondata.core.datastore.impl.FileFactory;
import org.apache.carbondata.core.util.CarbonProperties;

/**
Expand Down Expand Up @@ -72,4 +73,12 @@ protected void initRetry() {

}

public boolean releaseLockManually(String lockFile) {
try {
return FileFactory.deleteFile(lockFile, FileFactory.getFileType(lockFile));
} catch (Exception e) {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.carbondata.common.logging.LogService;
import org.apache.carbondata.common.logging.LogServiceFactory;
import org.apache.carbondata.core.constants.CarbonCommonConstants;
import org.apache.carbondata.core.datastore.filesystem.CarbonFile;
import org.apache.carbondata.core.datastore.impl.FileFactory;
import org.apache.carbondata.core.metadata.CarbonTableIdentifier;
import org.apache.carbondata.core.util.CarbonProperties;
Expand Down Expand Up @@ -102,34 +103,31 @@ public HdfsFileLock(CarbonTableIdentifier tableIdentifier, String lockFile) {
*/
@Override
public boolean unlock() {
boolean status = false;
if (null != dataOutputStream) {
try {
dataOutputStream.close();
status = true;
} catch (IOException e) {
try {
if (!FileFactory.isFileExist(location, FileFactory.getFileType(location))) {
return true;
}
} catch (IOException e1) {
LOGGER.error("Exception in isFileExist of the lock file " + e1.getMessage());
}
LOGGER.error("Exception in unlocking of the lock file " + e.getMessage());
return false;
status = false;
} finally {
try {
if (FileFactory.isFileExist(location, FileFactory.getFileType(location))) {
if (FileFactory.getCarbonFile(location, FileFactory.getFileType(location)).delete()) {
LOGGER.info("Deleted the lock file " + location);
} else {
LOGGER.error("Not able to delete the lock file " + location);
}
CarbonFile carbonFile =
FileFactory.getCarbonFile(location, FileFactory.getFileType(location));
if (carbonFile.exists()) {
if (carbonFile.delete()) {
LOGGER.info("Deleted the lock file " + location);
} else {
LOGGER.error("Not able to delete the lock file " + location);
status = false;
}
} catch (IOException e) {
LOGGER.error("Exception in isFileExist of the lock file " + e.getMessage());
} else {
LOGGER.error("Not able to delete the lock file because "
+ "it is not existed in location " + location);
status = false;
}
}
}
return true;
return status;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public interface ICarbonLock {
*/
boolean lockWithRetries();

/**
* This method will delete the lock file at the specified location.
*
* @param lockFile The path of the lock file.
* @return True if the lock file is deleted, false otherwise.
*/
boolean releaseLockManually(String lockFile);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.carbondata.core.constants.CarbonCommonConstants;
import org.apache.carbondata.core.datastore.impl.FileFactory;
import org.apache.carbondata.core.metadata.CarbonTableIdentifier;
import org.apache.carbondata.core.util.CarbonProperties;

/**
* This class handles the file locking in the local file system.
Expand Down Expand Up @@ -70,7 +71,8 @@ public class LocalFileLock extends AbstractCarbonLock {
LogServiceFactory.getLogService(LocalFileLock.class.getName());

static {
tmpPath = System.getProperty("java.io.tmpdir");
tmpPath = CarbonProperties.getInstance().getProperty(CarbonCommonConstants.STORE_LOCATION,
System.getProperty("java.io.tmpdir"));
}

/**
Expand Down Expand Up @@ -151,6 +153,7 @@ public LocalFileLock(CarbonTableIdentifier tableIdentifier, String lockFile) {
LOGGER.info("Successfully deleted the lock file " + lockFilePath);
} else {
LOGGER.error("Not able to delete the lock file " + lockFilePath);
status = false;
}
} catch (IOException e) {
LOGGER.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import org.apache.carbondata.core.metadata.{CarbonMetadata, CarbonTableIdentifie
import org.apache.carbondata.core.metadata.converter.ThriftWrapperSchemaConverterImpl
import org.apache.carbondata.core.metadata.encoder.Encoding
import org.apache.carbondata.core.metadata.schema.table.column.CarbonColumn
import org.apache.carbondata.core.util.CarbonUtil
import org.apache.carbondata.core.util.{CarbonProperties, CarbonUtil}
import org.apache.carbondata.core.util.path.CarbonStorePath
import org.apache.carbondata.format.{ColumnSchema, SchemaEvolutionEntry, TableInfo}
import org.apache.carbondata.spark.exception.MalformedCarbonCommandException
Expand Down Expand Up @@ -204,7 +204,18 @@ private[sql] case class AlterTableRenameTable(alterTableRenameModel: AlterTableR
if (carbonLock.unlock()) {
LOGGER.info("Lock released successfully")
} else {
LOGGER.error("Unable to release lock during rename table")
val storeLocation = CarbonProperties.getInstance
.getProperty(CarbonCommonConstants.STORE_LOCATION,
System.getProperty("java.io.tmpdir"))
val lockFilePath = storeLocation + CarbonCommonConstants.FILE_SEPARATOR +
oldDatabaseName + CarbonCommonConstants.FILE_SEPARATOR + newTableName +
CarbonCommonConstants.FILE_SEPARATOR +
LockUsage.METADATA_LOCK
if(carbonLock.releaseLockManually(lockFilePath)) {
LOGGER.info("Lock released successfully")
} else {
LOGGER.error("Unable to release lock during rename table")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.apache.carbondata.core.metadata.schema.table.CarbonTable
import org.apache.carbondata.format.{SchemaEvolutionEntry, TableInfo}

object AlterTableUtil {

def validateTableAndAcquireLock(dbName: String, tableName: String, LOGGER: LogService)
(sparkSession: SparkSession): ICarbonLock = {
val relation =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,19 @@ class AlterTableValidationTestCase extends QueryTest with BeforeAndAfterAll {
sql("drop database testdb")
}

test("test to check if the lock file is successfully deleted") {
sql("create table lock_check(id int, name string) stored by 'carbondata'")
sql("alter table lock_check rename to lock_rename")
assert(!new File(s"${ CarbonCommonConstants.STORE_LOCATION } + /default/lock_rename/meta.lock")
.exists())
}

override def afterAll {
sql("DROP TABLE IF EXISTS restructure")
sql("DROP TABLE IF EXISTS restructure_new")
sql("DROP TABLE IF EXISTS restructure_test")
sql("DROP TABLE IF EXISTS restructure_bad")
sql("DROP TABLE IF EXISTS restructure_badnew")
sql("DROP TABLE IF EXISTS lock_rename")
}
}

0 comments on commit 7ae1cd8

Please sign in to comment.