Skip to content

Commit 1bf0d09

Browse files
authored
Merge pull request douban#104 from mckelvin/bugfix-flush_all
golibmc: Fix ToggleFlushAllFeature in case no conns
2 parents 5ccc40c + 653db2a commit 1bf0d09

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

src/golibmc.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,12 @@ type Client struct {
128128
// maybeOpenNewConnections sends on the chan (one send per needed connection)
129129
// It is closed during client.Quit(). The close tells the connectionOpener
130130
// goroutine to exit.
131-
openerCh chan struct{}
132-
maxLifetime time.Duration // maximum amount of time a connection may be reused
133-
maxOpen int // maximum amount of connection num. maxOpen <= 0 means unlimited. default is 1.
134-
cleanerCh chan struct{}
135-
closed bool
131+
openerCh chan struct{}
132+
maxLifetime time.Duration // maximum amount of time a connection may be reused
133+
maxOpen int // maximum amount of connection num. maxOpen <= 0 means unlimited. default is 1.
134+
cleanerCh chan struct{}
135+
closed bool
136+
flushAllEnabled bool
136137
}
137138

138139
// connRequest represents one request for a new connection
@@ -216,6 +217,7 @@ func New(servers []string, noreply bool, prefix string, hashFunc int, failover b
216217
client.disableLock = disableLock
217218
client.hashFunc = hashFunc
218219
client.failover = failover
220+
client.flushAllEnabled = false
219221

220222
client.openerCh = make(chan struct{}, connectionRequestQueueSize)
221223
client.connRequests = make(map[uint64]chan connRequest)
@@ -1393,12 +1395,7 @@ func (client *Client) Stats() (map[string](map[string]string), error) {
13931395

13941396
// Enable/Disable the flush_all feature
13951397
func (client *Client) ToggleFlushAllFeature(enabled bool) {
1396-
client.lk.Lock()
1397-
for i := 0; i < len(client.freeConns); i++ {
1398-
cn := client.freeConns[i]
1399-
C.client_toggle_flush_all_feature(cn._imp, C.bool(enabled))
1400-
}
1401-
client.lk.Unlock()
1398+
client.flushAllEnabled = enabled
14021399
}
14031400

14041401
// FlushAll will flush all memcached servers
@@ -1410,6 +1407,9 @@ func (client *Client) FlushAll() ([]string, error) {
14101407
flushedHosts := []string{}
14111408

14121409
cn, err := client.conn(context.Background())
1410+
C.client_toggle_flush_all_feature(
1411+
cn._imp, C.bool(client.flushAllEnabled),
1412+
)
14131413
if err != nil {
14141414
return flushedHosts, err
14151415
}
@@ -1431,6 +1431,11 @@ func (client *Client) FlushAll() ([]string, error) {
14311431
}
14321432

14331433
if errCode != C.RET_OK {
1434+
if errCode == C.RET_PROGRAMMING_ERR {
1435+
return flushedHosts, errors.New(
1436+
"client.ToggleFlushAllFeature(true) to enable flush_all",
1437+
)
1438+
}
14341439
return flushedHosts, networkError(errorMessage(errCode))
14351440
}
14361441

src/golibmc_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,19 @@ func TestFlushAll(t *testing.T) {
916916
}
917917

918918
func testFlushAll(mc *Client, t *testing.T) {
919+
// Check if flush_all is disabled by default
920+
flushedHosts, err := mc.FlushAll()
921+
if !(err != nil && len(flushedHosts) == 0) {
922+
t.Error(err)
923+
}
924+
mc.ToggleFlushAllFeature(true)
925+
// Make sure we can flush directly
926+
flushedHosts, err = mc.FlushAll()
927+
if err != nil || len(flushedHosts) != len(mc.servers) {
928+
t.Error(err)
929+
}
930+
931+
// Set some random data
919932
nItems := 10
920933
items := make([]*Item, nItems)
921934
keys := make([]string, nItems)
@@ -931,28 +944,21 @@ func testFlushAll(mc *Client, t *testing.T) {
931944
}
932945
keys[i] = key
933946
}
934-
935947
mc.SetMulti(items)
936948

937-
// flush_all is disabled by default
938-
flushedHosts, err := mc.FlushAll()
939-
fmt.Println(err)
940-
if ! (err != nil && len(flushedHosts) == 0) {
941-
t.Error(err)
942-
}
943-
949+
// Make sure we have some data in the cache cluster
944950
itemsGot, err := mc.GetMulti(keys)
945951
if err != nil || len(itemsGot) != nItems {
946952
t.Error(err)
947953
}
948954

949-
// to enable flush_all, call the following method
950-
mc.ToggleFlushAllFeature(true)
955+
// Flush the cache cluster
951956
flushedHosts, err = mc.FlushAll()
952957
if err != nil || len(flushedHosts) != len(mc.servers) {
953958
t.Error(err)
954959
}
955960

961+
// Check if the cache cluster is flushed
956962
itemsGot, err = mc.GetMulti(keys)
957963
if err != ErrCacheMiss || len(itemsGot) != 0 {
958964
t.Error(err)

0 commit comments

Comments
 (0)