Skip to content

Commit 27cb040

Browse files
authored
Merge pull request #33 from ryogrid/shutdown-with-SIGINT-signal-impl
implemented server shutdown with SIGINT and avoiding needless index data reconstruction of index data at relaunch
2 parents ad2449f + 8e3a7e7 commit 27cb040

10 files changed

Lines changed: 114 additions & 38 deletions

File tree

lib/common/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var LogTimeout time.Duration
1313
const EnableDebug bool = false //true
1414

1515
// use virtual storage or not
16-
const EnableOnMemStorage = true
16+
const EnableOnMemStorage = false //true
1717

1818
// when this is true, virtual storage use is suppressed
1919
// for test case which can't work with virtual storage

lib/container/skip_list/skip_list_test/skip_list_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestSerializationOfSkipLisBlockPage(t *testing.T) {
5252
testingpkg.SimpleAssert(t, entry.Key.CompareEquals(types.NewVarchar("abcdeff")))
5353
testingpkg.SimpleAssert(t, entry.Value == 12345)
5454

55-
shi.Shutdown(false)
55+
shi.Shutdown(samehada.ShutdownPatternCloseFiles)
5656
}
5757

5858
func TestSerializationOfSkipLisHeaderPage(t *testing.T) {
@@ -76,7 +76,7 @@ func TestSerializationOfSkipLisHeaderPage(t *testing.T) {
7676
testingpkg.SimpleAssert(t, hpage.GetListStartPageId() == 7)
7777
testingpkg.SimpleAssert(t, hpage.GetKeyType() == types.Varchar)
7878

79-
shi.Shutdown(false)
79+
shi.Shutdown(samehada.ShutdownPatternCloseFiles)
8080
}
8181

8282
func TestInnerInsertDeleteOfBlockPageSimple(t *testing.T) {
@@ -178,7 +178,7 @@ func TestInnerInsertDeleteOfBlockPageSimple(t *testing.T) {
178178

179179
bpm.UnpinPage(bpage2.GetPageId(), true)
180180

181-
shi.Shutdown(false)
181+
shi.Shutdown(samehada.ShutdownPatternCloseFiles)
182182
}
183183

184184
func TestBSearchOfSkipLisBlockPage(t *testing.T) {
@@ -239,7 +239,7 @@ func TestBSearchOfSkipLisBlockPage(t *testing.T) {
239239
}
240240
}
241241

242-
shi.Shutdown(false)
242+
shi.Shutdown(samehada.ShutdownPatternCloseFiles)
243243
}
244244

245245
func TestBSearchOfSkipLisBlockPage2(t *testing.T) {
@@ -310,7 +310,7 @@ func TestBSearchOfSkipLisBlockPage2(t *testing.T) {
310310
}
311311
bpage.WUnlatch()
312312

313-
shi.Shutdown(false)
313+
shi.Shutdown(samehada.ShutdownPatternCloseFiles)
314314
}
315315

316316
func confirmSkipListContent(t *testing.T, sl *skip_list.SkipList, step int32) int32 {
@@ -432,7 +432,7 @@ func TestSkipListInsertAndDeleteAll(t *testing.T) {
432432
testingpkg.SimpleAssert(t, math.MaxUint64 == res)
433433
}
434434
435-
shi.Shutdown(false)
435+
shi.Shutdown(ShutdownPatternCloseFiles)
436436
}
437437
438438
func TestSkipListItr(t *testing.T) {

lib/execution/executors/executor_test/executor_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,7 +1809,7 @@ func TestSimpleAggregation(t *testing.T) {
18091809
testingpkg.Assert(t, tuple_ == nil && done == true && err == nil, "second call of AggregationExecutor::Next() failed")
18101810

18111811
txn_mgr.Commit(nil, txn)
1812-
shi.Shutdown(true)
1812+
shi.Shutdown(samehada.ShutdownPatternRemoveFiles)
18131813
}
18141814

18151815
func TestSimpleGroupByAggregation(t *testing.T) {
@@ -1889,7 +1889,7 @@ func TestSimpleGroupByAggregation(t *testing.T) {
18891889
}
18901890

18911891
txn_mgr.Commit(nil, txn)
1892-
shi.Shutdown(true)
1892+
shi.Shutdown(samehada.ShutdownPatternRemoveFiles)
18931893
}
18941894

18951895
func TestSeqScanWithMultiItemPredicate(t *testing.T) {
@@ -1963,7 +1963,7 @@ func TestSeqScanWithMultiItemPredicate(t *testing.T) {
19631963

19641964
txn_mgr.Commit(nil, txn)
19651965

1966-
shi.Shutdown(true)
1966+
shi.Shutdown(samehada.ShutdownPatternRemoveFiles)
19671967
}
19681968

19691969
func TestInsertAndSpecifiedColumnUpdate(t *testing.T) {
@@ -2138,7 +2138,7 @@ func TestInsertAndSpecifiedColumnUpdatePageMoveCase(t *testing.T) {
21382138
testingpkg.Assert(t, types.NewInteger(99).CompareEquals(results[0].GetValue(outSchema, 0)), "value should be 99")
21392139
testingpkg.Assert(t, types.NewVarchar("updated_xxxxxxxxxxxxxxxxxxxxxxxxx").CompareEquals(results[0].GetValue(outSchema, 1)), "value should be 'updated_xxxxxxxxxxxxxxxxxxxxxxxxx'")
21402140

2141-
shi.Shutdown(true)
2141+
shi.Shutdown(samehada.ShutdownPatternRemoveFiles)
21422142
}
21432143

21442144
func TestInsertAndSpecifiedColumnUpdatePageMoveRecovery(t *testing.T) {
@@ -2206,7 +2206,7 @@ func TestInsertAndSpecifiedColumnUpdatePageMoveRecovery(t *testing.T) {
22062206
executionEngine.Execute(updatePlanNode, executorContext)
22072207

22082208
// system crash before finish txn
2209-
shi.Shutdown(false)
2209+
shi.Shutdown(samehada.ShutdownPatternCloseFiles)
22102210

22112211
// restart system
22122212
shi = samehada.NewSamehadaInstance(t.Name(), common.BufferPoolMaxFrameNumForTest)
@@ -2269,7 +2269,7 @@ func TestInsertAndSpecifiedColumnUpdatePageMoveRecovery(t *testing.T) {
22692269
testingpkg.Assert(t, types.NewInteger(99).CompareEquals(results[0].GetValue(outSchema, 0)), "value should be 99")
22702270
testingpkg.Assert(t, types.NewVarchar("k").CompareEquals(results[0].GetValue(outSchema, 1)), "value should be 'k'")
22712271

2272-
shi.Shutdown(true)
2272+
shi.Shutdown(samehada.ShutdownPatternRemoveFiles)
22732273

22742274
common.TempSuppressOnMemStorage = false
22752275
common.TempSuppressOnMemStorageMutex.Unlock()
@@ -2381,7 +2381,7 @@ func TestInsertAndSpecifiedColumnUpdatePageMoveOccurOnRecovery(t *testing.T) {
23812381
txn_mgr.Commit(nil, txn2)
23822382

23832383
// system crash before finish "txn"
2384-
shi.Shutdown(false)
2384+
shi.Shutdown(samehada.ShutdownPatternCloseFiles)
23852385

23862386
// restart system
23872387
shi = samehada.NewSamehadaInstance(t.Name(), common.BufferPoolMaxFrameNumForTest)
@@ -2444,7 +2444,7 @@ func TestInsertAndSpecifiedColumnUpdatePageMoveOccurOnRecovery(t *testing.T) {
24442444
testingpkg.Assert(t, types.NewInteger(180).CompareEquals(results[0].GetValue(outSchema, 0)), "value should be 180")
24452445
testingpkg.Assert(t, types.NewVarchar("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk").CompareEquals(results[0].GetValue(outSchema, 1)), "value should be 'kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk'")
24462446

2447-
shi.Shutdown(true)
2447+
shi.Shutdown(samehada.ShutdownPatternRemoveFiles)
24482448

24492449
common.TempSuppressOnMemStorage = false
24502450
common.TempSuppressOnMemStorageMutex.Unlock()
@@ -2556,7 +2556,7 @@ func TestSimpleSeqScanAndOrderBy(t *testing.T) {
25562556
testingpkg.Assert(t, types.NewVarchar("daylight").CompareEquals(results[2].GetValue(scan_schema, 1)), "value should be 'daylight'")
25572557

25582558
txn_mgr.Commit(nil, txn)
2559-
shi.Shutdown(true)
2559+
shi.Shutdown(samehada.ShutdownPatternRemoveFiles)
25602560
}
25612561

25622562
func TestSimpleSetNullToVarchar(t *testing.T) {
@@ -2640,7 +2640,7 @@ func TestSimpleSetNullToVarchar(t *testing.T) {
26402640
testingpkg.Assert(t, types.NewVarchar("daylight").CompareEquals(results[2].GetValue(scan_schema, 1)), "value should be 'daylight'")
26412641

26422642
txn_mgr.Commit(nil, txn)
2643-
shi.Shutdown(true)
2643+
shi.Shutdown(samehada.ShutdownPatternRemoveFiles)
26442644
}
26452645

26462646
func TestInsertNullValueAndSeqScanWithNullComparison(t *testing.T) {

lib/recovery/recovery_test/log_recovery_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestRedo(t *testing.T) {
131131
fmt.Println("Tearing down the system..")
132132

133133
common.TempSuppressOnMemStorage = false
134-
samehada_instance.Shutdown(true)
134+
samehada_instance.Shutdown(samehada.ShutdownPatternRemoveFiles)
135135
common.TempSuppressOnMemStorageMutex.Unlock()
136136
}
137137

@@ -212,7 +212,7 @@ func TestUndo(t *testing.T) {
212212

213213
fmt.Println("System crash before commit")
214214
// delete samehada_instance
215-
samehada_instance.Shutdown(false)
215+
samehada_instance.Shutdown(samehada.ShutdownPatternCloseFiles)
216216

217217
fmt.Println("System restarted..")
218218
samehada_instance = samehada.NewSamehadaInstance(t.Name(), common.BufferPoolMaxFrameNumForTest)
@@ -280,7 +280,7 @@ func TestUndo(t *testing.T) {
280280
fmt.Println("Tearing down the system..")
281281

282282
common.TempSuppressOnMemStorage = false
283-
samehada_instance.Shutdown(true)
283+
samehada_instance.Shutdown(samehada.ShutdownPatternRemoveFiles)
284284
common.TempSuppressOnMemStorageMutex.Unlock()
285285
}
286286

@@ -398,7 +398,7 @@ func TestCheckpoint(t *testing.T) {
398398
fmt.Println("Tearing down the system..")
399399

400400
common.TempSuppressOnMemStorage = false
401-
samehada_instance.Shutdown(true)
401+
samehada_instance.Shutdown(samehada.ShutdownPatternRemoveFiles)
402402
common.TempSuppressOnMemStorageMutex.Unlock()
403403
}
404404

lib/samehada/request_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (reqManager *RequestManager) Run() {
9999
}
100100
}
101101

102-
// check stop signal or new request
102+
// check stop signal_handle or new request
103103
if !reqManager.isExecutionActive {
104104
break
105105
}

lib/samehada/samehada.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ func ReconstructAllIndexData(c *catalog.Catalog, dman disk.DiskManager, txn *acc
107107

108108
func NewSamehadaDB(dbName string, memKBytes int) *SamehadaDB {
109109
isExistingDB := false
110+
isExistingLog := false
110111

111112
if !common.EnableOnMemStorage || common.TempSuppressOnMemStorage {
112113
isExistingDB = samehada_util.FileExists(dbName + ".db")
114+
isExistingLog = samehada_util.FileExists(dbName + ".log")
113115
}
114116

115117
bpoolSize := math.Floor(float64(memKBytes*1024) / float64(common.PageSize))
@@ -125,16 +127,17 @@ func NewSamehadaDB(dbName string, memKBytes int) *SamehadaDB {
125127
shi.GetDiskManager(),
126128
shi.GetBufferPoolManager(),
127129
shi.GetLogManager())
128-
greatestLSN, isRedoOccured := log_recovery.Redo(txn)
129-
isUndoOccured := log_recovery.Undo(txn)
130+
greatestLSN, _ := log_recovery.Redo(txn)
131+
log_recovery.Undo(txn)
130132

131133
dman := shi.GetDiskManager()
132134
dman.GCLogFile()
133135
shi.GetLogManager().SetNextLSN(greatestLSN + 1)
134136

135137
c = catalog.RecoveryCatalogFromCatalogPage(shi.GetBufferPoolManager(), shi.GetLogManager(), shi.GetLockManager(), txn)
136138

137-
if isRedoOccured || isUndoOccured {
139+
// db file exists but log file doesn't exist case means gracefully shutdown. so this block is passed
140+
if isExistingLog {
138141
// index date reloading/recovery is not implemented yet
139142
// so when db did not exit graceful, all index data should be recounstruct
140143
// (hash index uses already allocated pages but skip list index deserts these...)
@@ -239,11 +242,11 @@ func (sdb *SamehadaDB) Shutdown() {
239242
sdb.statistics_updator.StopStatsUpdateTh()
240243
sdb.shi_.GetCheckpointManager().StopCheckpointTh()
241244
sdb.request_manager.StopTh()
242-
isSuccess := sdb.shi_.GetBufferPoolManager().FlushAllDirtyPages()
243-
if !isSuccess {
244-
panic("flush all dirty pages failed!")
245-
}
246-
sdb.shi_.Shutdown(false)
245+
//isSuccess := sdb.shi_.GetBufferPoolManager().FlushAllDirtyPages()
246+
//if !isSuccess {
247+
// panic("flush all dirty pages failed!")
248+
//}
249+
sdb.shi_.Shutdown(ShutdownPatternRemoveLogOnly)
247250
}
248251

249252
// no flush of page buffer

lib/samehada/samehada_instance.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import (
99
"github.com/ryogrid/SamehadaDB/lib/storage/disk"
1010
)
1111

12+
type ShutdownPattern int
13+
14+
const (
15+
ShutdownPatternRemoveFiles ShutdownPattern = iota
16+
ShutdownPatternCloseFiles
17+
ShutdownPatternRemoveLogOnly
18+
)
19+
1220
type SamehadaInstance struct {
1321
disk_manager disk.DiskManager
1422
log_manager *recovery.LogManager
@@ -70,19 +78,28 @@ func (si *SamehadaInstance) GetCheckpointManager() *concurrency.CheckpointManage
7078
}
7179

7280
// functionality is Flushing dirty pages, shutdown of DiskManager and action around DB/Log files
73-
func (si *SamehadaInstance) Shutdown(IsRemoveFiles bool) {
74-
if IsRemoveFiles {
81+
func (si *SamehadaInstance) Shutdown(shutdownPat ShutdownPattern) {
82+
switch shutdownPat {
83+
case ShutdownPatternRemoveFiles:
7584
//close
7685
si.disk_manager.ShutDown()
7786
//remove
7887
si.disk_manager.RemoveDBFile()
7988
si.disk_manager.RemoveLogFile()
80-
} else {
89+
case ShutdownPatternCloseFiles:
8190
si.log_manager.Flush()
82-
// TODO: (SDB) flush only dirty pages
83-
si.bpm.FlushAllPages()
91+
si.bpm.FlushAllDirtyPages()
8492
// close only
8593
si.disk_manager.ShutDown()
94+
case ShutdownPatternRemoveLogOnly:
95+
si.log_manager.Flush()
96+
si.bpm.FlushAllDirtyPages()
97+
// close files
98+
si.disk_manager.ShutDown()
99+
100+
si.disk_manager.RemoveLogFile()
101+
default:
102+
panic("invalid shutdown pattern")
86103
}
87104
}
88105

lib/storage/index/index_test/hash_table_index_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func TestRecounstructionOfHashIndex(t *testing.T) {
154154
}
155155

156156
shi.GetTransactionManager().Commit(nil, txn)
157-
shi.Shutdown(false)
157+
shi.Shutdown(samehada.ShutdownPatternCloseFiles)
158158

159159
// ----------- check recovery includes index data ----------
160160

@@ -239,6 +239,6 @@ func TestRecounstructionOfHashIndex(t *testing.T) {
239239
shi.GetTransactionManager().Commit(nil, txn)
240240

241241
common.TempSuppressOnMemStorage = false
242-
shi.Shutdown(false)
242+
shi.Shutdown(samehada.ShutdownPatternCloseFiles)
243243
common.TempSuppressOnMemStorageMutex.Unlock()
244244
}

server/main.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"fmt"
55
"github.com/ant0ine/go-json-rest/rest"
66
"github.com/ryogrid/SamehadaDB/lib/samehada"
7+
"github.com/ryogrid/SamehadaDB/server/signal_handle"
78
"log"
89
"net/http"
10+
"os"
911
)
1012

1113
type QueryInput struct {
@@ -22,8 +24,14 @@ type QueryOutput struct {
2224
}
2325

2426
var db = samehada.NewSamehadaDB("default", 5000) //5MB
27+
var IsStopped = false
2528

2629
func postQuery(w rest.ResponseWriter, req *rest.Request) {
30+
if signal_handle.IsStopped {
31+
rest.Error(w, "Server is stopped", http.StatusGone)
32+
return
33+
}
34+
2735
input := QueryInput{}
2836
err := req.DecodeJsonPayload(&input)
2937

@@ -54,7 +62,7 @@ func postQuery(w rest.ResponseWriter, req *rest.Request) {
5462
})
5563
}
5664

57-
func main() {
65+
func launchDBAndListen() {
5866
api := rest.NewApi()
5967

6068
// the Middleware stack
@@ -87,3 +95,20 @@ func main() {
8795
api.MakeHandler(),
8896
))
8997
}
98+
99+
func main() {
100+
exitNotifyCh := make(chan bool, 1)
101+
102+
// start signal handler thread
103+
go signal_handle.SignalHandlerTh(db, &exitNotifyCh)
104+
105+
// start server
106+
go launchDBAndListen()
107+
108+
// wait shutdown operation finished notification
109+
<-exitNotifyCh
110+
111+
fmt.Println("Server is stopped gracefully")
112+
// exit process
113+
os.Exit(0)
114+
}

0 commit comments

Comments
 (0)