Unverified 提交 8a7aa9db authored 作者: Andrei Tokar's avatar Andrei Tokar 提交者: GitHub

Merge pull request #1194 from h2database/post-undo-split

TestKillRestartMulti: A map named undoLog-1 already exists
......@@ -808,7 +808,7 @@ public class Database implements DataHandler {
}
systemSession.commit(true);
if (mvStore != null) {
mvStore.initTransactions();
mvStore.getTransactionStore().endLeftoverTransactions();
mvStore.removeTemporaryMaps(objectIds);
}
recompileInvalidViews(systemSession);
......@@ -1014,6 +1014,7 @@ public class Database implements DataHandler {
SearchRow r = meta.getTemplateSimpleRow(false);
r.setValue(0, ValueInt.get(id));
boolean wasLocked = lockMeta(session);
try {
Cursor cursor = metaIdIndex.find(session, r, r);
if (cursor.next()) {
if (SysProperties.CHECK) {
......@@ -1031,12 +1032,13 @@ public class Database implements DataHandler {
if (SysProperties.CHECK) {
checkMetaFree(session, id);
}
} else if (!wasLocked) {
unlockMetaDebug(session);
}
} finally {
if (!wasLocked) {
// must not keep the lock if it was not locked
// otherwise updating sequences may cause a deadlock
meta.unlock(session);
session.unlock(meta);
unlockMeta(session);
}
}
objectIds.clear(id);
}
......@@ -1333,10 +1335,10 @@ public class Database implements DataHandler {
}
closing = true;
}
}
if(!this.isReadOnly()) {
if (!this.isReadOnly()) {
removeOrphanedLobs();
}
}
try {
if (systemSession != null) {
if (powerOffCount != -1) {
......@@ -1427,6 +1429,7 @@ public class Database implements DataHandler {
* @param flush whether writing is allowed
*/
private synchronized void closeOpenFilesAndUnlock(boolean flush) {
try {
stopWriter();
if (pageStore != null) {
if (flush) {
......@@ -1456,7 +1459,7 @@ public class Database implements DataHandler {
}
}
reconnectModified(false);
if (mvStore != null && !mvStore.getStore().isClosed()) {
if (mvStore != null && mvStore.getStore() != null && !mvStore.getStore().isClosed()) {
long maxCompactTime = dbSettings.maxCompactTime;
if (compactMode == CommandInterface.SHUTDOWN_COMPACT) {
mvStore.compactFile(dbSettings.maxCompactTime);
......@@ -1467,6 +1470,14 @@ public class Database implements DataHandler {
}
mvStore.close(maxCompactTime);
}
if (systemSession != null) {
systemSession.close();
systemSession = null;
}
if (lobSession != null) {
lobSession.close();
lobSession = null;
}
closeFiles();
if (persistent && lock == null &&
fileLockMethod != FileLockMethod.NO &&
......@@ -1479,14 +1490,7 @@ public class Database implements DataHandler {
if (persistent) {
deleteOldTempFiles();
}
if (systemSession != null) {
systemSession.close();
systemSession = null;
}
if (lobSession != null) {
lobSession.close();
lobSession = null;
}
} finally {
if (lock != null) {
if (fileLockMethod == FileLockMethod.SERIALIZED) {
// wait before deleting the .lock file,
......@@ -1504,6 +1508,7 @@ public class Database implements DataHandler {
lock = null;
}
}
}
private synchronized void closeFiles() {
try {
......@@ -1946,7 +1951,6 @@ public class Database implements DataHandler {
t.getSQL());
}
obj.removeChildrenAndResources(session);
}
removeMeta(session, id);
}
......@@ -2550,6 +2554,7 @@ public class Database implements DataHandler {
* Immediately close the database.
*/
public void shutdownImmediately() {
closing = true;
setPowerOffCount(1);
try {
checkPowerOff();
......
......@@ -511,9 +511,12 @@ public class MVStore {
M map = (M) getMap(id);
if (map == null) {
String configAsString = meta.get(MVMap.getMapKey(id));
if(configAsString != null) {
HashMap<String, Object> config =
new HashMap<String, Object>(DataUtils.parseMap(configAsString));
HashMap<String, Object> config;
if (configAsString != null) {
config = new HashMap<String, Object>(DataUtils.parseMap(configAsString));
} else {
config = new HashMap<>();
}
config.put("id", id);
map = builder.create(this, config);
map.init();
......@@ -521,7 +524,6 @@ public class MVStore {
map.setRootPos(root, lastStoredVersion);
maps.put(id, map);
}
}
return map;
} finally {
storeLock.unlock();
......
......@@ -109,10 +109,7 @@ public class MVTableEngine implements TableEngine {
public TableBase createTable(CreateTableData data) {
Database db = data.session.getDatabase();
Store store = init(db);
MVTable table = new MVTable(data, store);
table.init(data.session);
store.tableMap.put(table.getMapName(), table);
return table;
return store.createTable(data);
}
/**
......@@ -124,7 +121,7 @@ public class MVTableEngine implements TableEngine {
* The map of open tables.
* Key: the map name, value: the table.
*/
final ConcurrentHashMap<String, MVTable> tableMap =
private final ConcurrentHashMap<String, MVTable> tableMap =
new ConcurrentHashMap<>();
/**
......@@ -152,7 +149,7 @@ public class MVTableEngine implements TableEngine {
* @param builder the builder
* @param encrypted whether the store is encrypted
*/
void open(Database db, MVStore.Builder builder, boolean encrypted) {
private void open(Database db, MVStore.Builder builder, boolean encrypted) {
this.encrypted = encrypted;
try {
this.store = builder.open();
......@@ -166,7 +163,6 @@ public class MVTableEngine implements TableEngine {
this.transactionStore = new TransactionStore(
store,
new ValueDataType(db.getCompareMode(), db, null), db.getLockTimeout());
// transactionStore.init();
} catch (IllegalStateException e) {
throw convertIllegalStateException(e);
}
......@@ -195,6 +191,10 @@ public class MVTableEngine implements TableEngine {
throw DbException.get(
ErrorCode.IO_EXCEPTION_1,
e, fileName);
} else if (errorCode == DataUtils.ERROR_INTERNAL) {
throw DbException.get(
ErrorCode.GENERAL_ERROR_1,
e, fileName);
}
throw DbException.get(
ErrorCode.FILE_CORRUPTED_1,
......@@ -214,6 +214,19 @@ public class MVTableEngine implements TableEngine {
return tableMap.get(tableName);
}
/**
* Create a table.
*
* @param data CreateTableData
* @return table created
*/
public MVTable createTable(CreateTableData data) {
MVTable table = new MVTable(data, this);
table.init(data.session);
tableMap.put(table.getMapName(), table);
return table;
}
/**
* Remove a table.
*
......@@ -246,21 +259,6 @@ public class MVTableEngine implements TableEngine {
store.closeImmediately();
}
/**
* Commit all transactions that are in the committing state, and
* rollback all open transactions.
*/
public void initTransactions() {
List<Transaction> list = transactionStore.getOpenTransactions();
for (Transaction t : list) {
if (t.getStatus() == Transaction.STATUS_COMMITTED) {
t.commit();
} else if (t.getStatus() != Transaction.STATUS_PREPARED) {
t.rollback();
}
}
}
/**
* Remove all temporary maps.
*
......
......@@ -436,7 +436,7 @@ public class Transaction {
return store.getChanges(this, getLogId(), savepointId);
}
long getLogId() {
private long getLogId() {
return getLogId(statusAndLogId.get());
}
......@@ -454,7 +454,7 @@ public class Transaction {
/**
* Check whether this transaction is open or prepared.
*/
void checkNotClosed() {
private void checkNotClosed() {
if (getStatus() == STATUS_CLOSED) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_CLOSED, "Transaction {0} is closed", transactionId);
......
......@@ -89,11 +89,6 @@ public class TransactionStore {
private final AtomicReferenceArray<Transaction> transactions =
new AtomicReferenceArray<>(MAX_OPEN_TRANSACTIONS + 1);
/**
* The next id of a temporary map.
*/
private int nextTempMapId;
private static final String UNDO_LOG_NAME_PEFIX = "undoLog";
private static final char UNDO_LOG_COMMITTED = '-'; // must come before open in lexicographical order
private static final char UNDO_LOG_OPEN = '.';
......@@ -147,14 +142,6 @@ public class TransactionStore {
*/
public void init() {
if (!init) {
// remove all temporary maps
for (String mapName : store.getMapNames()) {
if (mapName.startsWith("temp.")) {
MVMap<Object, Integer> temp = openTempMap(mapName);
store.removeMap(temp);
}
}
for (String mapName : store.getMapNames()) {
if (mapName.startsWith(UNDO_LOG_NAME_PEFIX)) {
if (store.hasData(mapName)) {
......@@ -165,13 +152,15 @@ public class TransactionStore {
int status;
String name;
if (data == null) {
status = mapName.charAt(UNDO_LOG_NAME_PEFIX.length()) == UNDO_LOG_OPEN ?
Transaction.STATUS_OPEN : Transaction.STATUS_COMMITTED;
status = Transaction.STATUS_OPEN;
name = null;
} else {
status = (Integer) data[0];
name = (String) data[1];
}
if (mapName.charAt(UNDO_LOG_NAME_PEFIX.length()) == UNDO_LOG_COMMITTED) {
status = Transaction.STATUS_COMMITTED;
}
MVMap<Long, Object[]> undoLog = store.openMap(mapName, undoLogBuilder);
undoLogs[transactionId] = undoLog;
Long lastUndoKey = undoLog.lastKey();
......@@ -187,6 +176,21 @@ public class TransactionStore {
}
}
/**
* Commit all transactions that are in the committed state, and
* rollback all open transactions.
*/
public void endLeftoverTransactions() {
List<Transaction> list = getOpenTransactions();
for (Transaction t : list) {
if (t.getStatus() == Transaction.STATUS_COMMITTED) {
t.commit();
} else if (t.getStatus() != Transaction.STATUS_PREPARED) {
t.rollback();
}
}
}
/**
* Set the maximum transaction id, after which ids are re-used. If the old
* transaction is still in use when re-using an old id, the new transaction
......@@ -508,29 +512,6 @@ public class TransactionStore {
return map;
}
/**
* Create a temporary map. Such maps are removed when opening the store.
*
* @return the map
*/
synchronized MVMap<Object, Integer> createTempMap() {
String mapName = "temp." + nextTempMapId++;
return openTempMap(mapName);
}
/**
* Open a temporary map.
*
* @param mapName the map name
* @return the map
*/
private MVMap<Object, Integer> openTempMap(String mapName) {
MVMap.Builder<Object, Integer> mapBuilder =
new MVMap.Builder<Object, Integer>().
keyType(dataType);
return store.openMap(mapName, mapBuilder);
}
/**
* End this transaction. Change status to CLOSED and vacate transaction slot.
* Will try to commit MVStore if autocommitDelay is 0 or if database is idle
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论