-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
etcdserver: Extract functions for setting and reading compaction info…
…rmation in backend
- Loading branch information
Showing
6 changed files
with
166 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2015 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mvcc | ||
|
||
import ( | ||
"go.etcd.io/etcd/server/v3/mvcc/backend" | ||
"go.etcd.io/etcd/server/v3/mvcc/buckets" | ||
) | ||
|
||
func UnsafeReadFinishedmpact(tx backend.ReadTx) (finishedComact int64, found bool) { | ||
_, finishedCompactBytes := tx.UnsafeRange(buckets.Meta, buckets.FinishedCompactKeyName, nil, 0) | ||
if len(finishedCompactBytes) != 0 { | ||
return bytesToRev(finishedCompactBytes[0]).main, true | ||
} | ||
return 0, false | ||
} | ||
|
||
func UnsafeReadScheduledCompact(tx backend.ReadTx) (scheduledComact int64, found bool) { | ||
_, scheduledCompactBytes := tx.UnsafeRange(buckets.Meta, buckets.ScheduledCompactKeyName, nil, 0) | ||
if len(scheduledCompactBytes) != 0 { | ||
return bytesToRev(scheduledCompactBytes[0]).main, true | ||
} | ||
return 0, false | ||
} | ||
|
||
func SetScheduledCompact(tx backend.BatchTx, value int64) { | ||
tx.Lock() | ||
defer tx.Unlock() | ||
UnsafeSetScheduledCompact(tx, value) | ||
} | ||
|
||
func UnsafeSetScheduledCompact(tx backend.BatchTx, value int64) { | ||
rbytes := newRevBytes() | ||
revToBytes(revision{main: value}, rbytes) | ||
tx.UnsafePut(buckets.Meta, buckets.ScheduledCompactKeyName, rbytes) | ||
} | ||
|
||
func SetFinishedCompact(tx backend.BatchTx, value int64) { | ||
tx.Lock() | ||
defer tx.Unlock() | ||
UnsafeSetFinishedCompact(tx, value) | ||
} | ||
|
||
func UnsafeSetFinishedCompact(tx backend.BatchTx, value int64) { | ||
rbytes := newRevBytes() | ||
revToBytes(revision{main: value}, rbytes) | ||
tx.UnsafePut(buckets.Meta, buckets.FinishedCompactKeyName, rbytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package mvcc | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.etcd.io/etcd/server/v3/mvcc/backend" | ||
betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing" | ||
"go.etcd.io/etcd/server/v3/mvcc/buckets" | ||
) | ||
|
||
// TestScheduledCompact ensures that UnsafeSetScheduledCompact&UnsafeReadScheduledCompact work well together. | ||
func TestScheduledCompact(t *testing.T) { | ||
tcs := []struct { | ||
value int64 | ||
}{ | ||
{ | ||
value: 1, | ||
}, | ||
{ | ||
value: 0, | ||
}, | ||
{ | ||
value: math.MaxInt64, | ||
}, | ||
{ | ||
value: math.MinInt64, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(fmt.Sprint(tc.value), func(t *testing.T) { | ||
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10) | ||
tx := be.BatchTx() | ||
if tx == nil { | ||
t.Fatal("batch tx is nil") | ||
} | ||
tx.Lock() | ||
tx.UnsafeCreateBucket(buckets.Meta) | ||
UnsafeSetScheduledCompact(tx, tc.value) | ||
tx.Unlock() | ||
be.ForceCommit() | ||
be.Close() | ||
|
||
b := backend.NewDefaultBackend(tmpPath) | ||
defer b.Close() | ||
v, found := UnsafeReadScheduledCompact(b.BatchTx()) | ||
assert.Equal(t, true, found) | ||
assert.Equal(t, tc.value, v) | ||
}) | ||
} | ||
} | ||
|
||
// TestFinishedCompact ensures that UnsafeSetFinishedCompact&UnsafeReadFinishedmpact work well together. | ||
func TestFinishedCompact(t *testing.T) { | ||
tcs := []struct { | ||
value int64 | ||
}{ | ||
{ | ||
value: 1, | ||
}, | ||
{ | ||
value: 0, | ||
}, | ||
{ | ||
value: math.MaxInt64, | ||
}, | ||
{ | ||
value: math.MinInt64, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(fmt.Sprint(tc.value), func(t *testing.T) { | ||
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10) | ||
tx := be.BatchTx() | ||
if tx == nil { | ||
t.Fatal("batch tx is nil") | ||
} | ||
tx.Lock() | ||
tx.UnsafeCreateBucket(buckets.Meta) | ||
UnsafeSetFinishedCompact(tx, tc.value) | ||
tx.Unlock() | ||
be.ForceCommit() | ||
be.Close() | ||
|
||
b := backend.NewDefaultBackend(tmpPath) | ||
defer b.Close() | ||
v, found := UnsafeReadFinishedmpact(b.BatchTx()) | ||
assert.Equal(t, true, found) | ||
assert.Equal(t, tc.value, v) | ||
}) | ||
} | ||
} |