提交 daa433f0 authored 作者: Andrei Tokar's avatar Andrei Tokar

Exception handling improvements

Assorted minor changes
上级 f796e624
......@@ -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,29 +1014,31 @@ public class Database implements DataHandler {
SearchRow r = meta.getTemplateSimpleRow(false);
r.setValue(0, ValueInt.get(id));
boolean wasLocked = lockMeta(session);
Cursor cursor = metaIdIndex.find(session, r, r);
if (cursor.next()) {
if (SysProperties.CHECK) {
if (lockMode != Constants.LOCK_MODE_OFF && !wasLocked) {
throw DbException.throwInternalError();
try {
Cursor cursor = metaIdIndex.find(session, r, r);
if (cursor.next()) {
if (SysProperties.CHECK) {
if (lockMode != Constants.LOCK_MODE_OFF && !wasLocked) {
throw DbException.throwInternalError();
}
}
Row found = cursor.get();
meta.removeRow(session, found);
if (isMultiVersion()) {
// TODO this should work without MVCC, but avoid risks at
// the moment
session.log(meta, UndoLogRecord.DELETE, found);
}
if (SysProperties.CHECK) {
checkMetaFree(session, id);
}
}
Row found = cursor.get();
meta.removeRow(session, found);
if (isMultiVersion()) {
// TODO this should work without MVCC, but avoid risks at
// the moment
session.log(meta, UndoLogRecord.DELETE, found);
}
if (SysProperties.CHECK) {
checkMetaFree(session, id);
} finally {
if (!wasLocked) {
// must not keep the lock if it was not locked
// otherwise updating sequences may cause a deadlock
unlockMeta(session);
}
} else if (!wasLocked) {
unlockMetaDebug(session);
// must not keep the lock if it was not locked
// otherwise updating sequences may cause a deadlock
meta.unlock(session);
session.unlock(meta);
}
objectIds.clear(id);
}
......@@ -1333,9 +1335,9 @@ public class Database implements DataHandler {
}
closing = true;
}
}
if(!this.isReadOnly()) {
removeOrphanedLobs();
if (!this.isReadOnly()) {
removeOrphanedLobs();
}
}
try {
if (systemSession != null) {
......@@ -1427,81 +1429,84 @@ public class Database implements DataHandler {
* @param flush whether writing is allowed
*/
private synchronized void closeOpenFilesAndUnlock(boolean flush) {
stopWriter();
if (pageStore != null) {
if (flush) {
try {
pageStore.checkpoint();
if (!readOnly) {
lockMeta(pageStore.getPageStoreSession());
pageStore.compact(compactMode);
unlockMeta(pageStore.getPageStoreSession());
}
} catch (DbException e) {
if (ASSERT) {
int code = e.getErrorCode();
if (code != ErrorCode.DATABASE_IS_CLOSED &&
code != ErrorCode.LOCK_TIMEOUT_1 &&
code != ErrorCode.IO_EXCEPTION_2) {
e.printStackTrace();
try {
stopWriter();
if (pageStore != null) {
if (flush) {
try {
pageStore.checkpoint();
if (!readOnly) {
lockMeta(pageStore.getPageStoreSession());
pageStore.compact(compactMode);
unlockMeta(pageStore.getPageStoreSession());
}
} catch (DbException e) {
if (ASSERT) {
int code = e.getErrorCode();
if (code != ErrorCode.DATABASE_IS_CLOSED &&
code != ErrorCode.LOCK_TIMEOUT_1 &&
code != ErrorCode.IO_EXCEPTION_2) {
e.printStackTrace();
}
}
trace.error(e, "close");
} catch (Throwable t) {
if (ASSERT) {
t.printStackTrace();
}
trace.error(t, "close");
}
trace.error(e, "close");
} catch (Throwable t) {
if (ASSERT) {
t.printStackTrace();
}
trace.error(t, "close");
}
}
}
reconnectModified(false);
if (mvStore != null && !mvStore.getStore().isClosed()) {
long maxCompactTime = dbSettings.maxCompactTime;
if (compactMode == CommandInterface.SHUTDOWN_COMPACT) {
mvStore.compactFile(dbSettings.maxCompactTime);
} else if (compactMode == CommandInterface.SHUTDOWN_DEFRAG) {
maxCompactTime = Long.MAX_VALUE;
} else if (getSettings().defragAlways) {
maxCompactTime = Long.MAX_VALUE;
reconnectModified(false);
if (mvStore != null && mvStore.getStore() != null && !mvStore.getStore().isClosed()) {
long maxCompactTime = dbSettings.maxCompactTime;
if (compactMode == CommandInterface.SHUTDOWN_COMPACT) {
mvStore.compactFile(dbSettings.maxCompactTime);
} else if (compactMode == CommandInterface.SHUTDOWN_DEFRAG) {
maxCompactTime = Long.MAX_VALUE;
} else if (getSettings().defragAlways) {
maxCompactTime = Long.MAX_VALUE;
}
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 &&
fileLockMethod != FileLockMethod.FS) {
// everything already closed (maybe in checkPowerOff)
// don't delete temp files in this case because
// the database could be open now (even from within another process)
return;
}
mvStore.close(maxCompactTime);
}
closeFiles();
if (persistent && lock == null &&
fileLockMethod != FileLockMethod.NO &&
fileLockMethod != FileLockMethod.FS) {
// everything already closed (maybe in checkPowerOff)
// don't delete temp files in this case because
// the database could be open now (even from within another process)
return;
}
if (persistent) {
deleteOldTempFiles();
}
if (systemSession != null) {
systemSession.close();
systemSession = null;
}
if (lobSession != null) {
lobSession.close();
lobSession = null;
}
if (lock != null) {
if (fileLockMethod == FileLockMethod.SERIALIZED) {
// wait before deleting the .lock file,
// otherwise other connections can not detect that
if (lock.load().containsKey("changePending")) {
try {
Thread.sleep(TimeUnit.NANOSECONDS
.toMillis((long) (reconnectCheckDelayNs * 1.1)));
} catch (InterruptedException e) {
trace.error(e, "close");
if (persistent) {
deleteOldTempFiles();
}
} finally {
if (lock != null) {
if (fileLockMethod == FileLockMethod.SERIALIZED) {
// wait before deleting the .lock file,
// otherwise other connections can not detect that
if (lock.load().containsKey("changePending")) {
try {
Thread.sleep(TimeUnit.NANOSECONDS
.toMillis((long) (reconnectCheckDelayNs * 1.1)));
} catch (InterruptedException e) {
trace.error(e, "close");
}
}
}
lock.unlock();
lock = null;
}
lock.unlock();
lock = null;
}
}
......@@ -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,16 +511,18 @@ 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));
config.put("id", id);
map = builder.create(this, config);
map.init();
long root = getRootPos(meta, id);
map.setRootPos(root, lastStoredVersion);
maps.put(id, map);
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();
long root = getRootPos(meta, id);
map.setRootPos(root, lastStoredVersion);
maps.put(id, map);
}
return map;
} finally {
......
......@@ -246,21 +246,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.
*
......
......@@ -147,14 +147,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 +157,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 +181,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
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论