@@ -325,7 +325,9 @@ func (c *CassandraStore) processWriteQueue(queue chan *mdata.ChunkWriteRequest,
325
325
case cwr := <- queue :
326
326
meter .Value (len (queue ))
327
327
keyStr := cwr .Key .String ()
328
- log .Debugf ("CS: starting to save %s:%d %v" , keyStr , cwr .T0 , cwr .Data )
328
+ if log .IsLevelEnabled (log .DebugLevel ) {
329
+ log .Debugf ("cassandra_store: starting to save %s:%d %v" , keyStr , cwr .T0 , cwr .Data )
330
+ }
329
331
//log how long the chunk waited in the queue before we attempted to save to cassandra
330
332
cassPutWaitDuration .Value (time .Now ().Sub (cwr .Timestamp ))
331
333
@@ -340,12 +342,14 @@ func (c *CassandraStore) processWriteQueue(queue chan *mdata.ChunkWriteRequest,
340
342
if cwr .Callback != nil {
341
343
cwr .Callback ()
342
344
}
343
- log .Debugf ("CS: save complete. %s:%d %v" , keyStr , cwr .T0 , cwr .Data )
345
+ if log .IsLevelEnabled (log .DebugLevel ) {
346
+ log .Debugf ("cassandra_store: save complete. %s:%d %v" , keyStr , cwr .T0 , cwr .Data )
347
+ }
344
348
chunkSaveOk .Inc ()
345
349
} else {
346
350
errmetrics .Inc (err )
347
351
if (attempts % 20 ) == 0 {
348
- log .Warnf ("CS : failed to save chunk to cassandra after %d attempts. %v, %s" , attempts + 1 , cwr .Data , err )
352
+ log .Warnf ("cassandra_store : failed to save chunk to cassandra after %d attempts. %v, %s" , attempts + 1 , cwr .Data , err )
349
353
}
350
354
chunkSaveFail .Inc ()
351
355
sleepTime := 100 * attempts
@@ -358,6 +362,9 @@ func (c *CassandraStore) processWriteQueue(queue chan *mdata.ChunkWriteRequest,
358
362
}
359
363
case <- c .shutdown :
360
364
log .Info ("cassandra_store: received shutdown, exiting processWriteQueue" )
365
+ if c .Session != nil && ! c .Session .Closed () {
366
+ c .Session .Close ()
367
+ }
361
368
return
362
369
}
363
370
}
@@ -446,6 +453,9 @@ func (c *CassandraStore) processReadQueue() {
446
453
crr .out <- iter
447
454
case <- c .shutdown :
448
455
log .Info ("cassandra_store: received shutdown, exiting processReadQueue" )
456
+ if c .Session != nil && ! c .Session .Closed () {
457
+ c .Session .Close ()
458
+ }
449
459
return
450
460
}
451
461
}
@@ -459,24 +469,42 @@ func (c *CassandraStore) deadConnectionCheck() {
459
469
for {
460
470
select {
461
471
case <- ticker .C :
472
+ c .sessionLock .RLock ()
462
473
err := c .Session .Query ("SELECT cql_version FROM system.local" ).Exec ()
463
- if err != nil {
474
+ if err == nil {
475
+ attempts = 0
476
+ } else {
464
477
attempts ++
465
478
log .Errorf ("cassandra_store: could not execute connection check query for %d attempts: %v" , attempts , err )
466
479
}
480
+ c .sessionLock .RUnlock ()
481
+
467
482
if attempts >= 6 {
483
+ success := false
468
484
attempts = 0
469
- log .Errorf ("cassandra_store: creating new session to cassandra using hosts: %v" , c .config .Addrs )
470
- c .Cluster .Hosts = strings .Split (c .config .Addrs , "," )
471
- c .Session .Close ()
472
- c .Session , err = c .Cluster .CreateSession ()
473
- if err != nil {
474
- log .Errorf ("cassandra_store: error while attempting to recreate cassandra session. will retry after %d seconds: %v" , time .Second * 30 , err )
475
- attempts ++
485
+ c .sessionLock .Lock ()
486
+ for ! success {
487
+ log .Errorf ("cassandra_store: creating new session to cassandra using hosts: %v" , c .config .Addrs )
488
+ // c.Cluster.Hosts = strings.Split(c.config.Addrs, ",")
489
+ if c .Session != nil && ! c .Session .Closed () {
490
+ c .Session .Close ()
491
+ c .Session = nil
492
+ }
493
+ c .Session , err = c .Cluster .CreateSession ()
494
+ if err != nil {
495
+ log .Errorf ("cassandra_store: error while attempting to recreate cassandra session. will retry after 5 seconds: %v" , err )
496
+ time .Sleep (time .Second * 5 )
497
+ } else {
498
+ success = true
499
+ }
476
500
}
501
+ c .sessionLock .Unlock ()
477
502
}
478
503
case <- c .shutdown :
479
504
log .Info ("cassandra_store: received shutdown, exiting deadConnectionCheck" )
505
+ if c .Session != nil && ! c .Session .Closed () {
506
+ c .Session .Close ()
507
+ }
480
508
return
481
509
}
482
510
}
@@ -554,6 +582,9 @@ func (c *CassandraStore) SearchTable(ctx context.Context, key schema.AMKey, tabl
554
582
ctx : ctx ,
555
583
}
556
584
585
+ c .sessionLock .RLock ()
586
+ defer c .sessionLock .RUnlock ()
587
+
557
588
select {
558
589
case <- ctx .Done ():
559
590
// request has been canceled, so no need to continue queuing reads.
@@ -624,7 +655,4 @@ func (c *CassandraStore) SearchTable(ctx context.Context, key schema.AMKey, tabl
624
655
func (c * CassandraStore ) Stop () {
625
656
close (c .shutdown )
626
657
c .wg .Wait ()
627
- if c .Session != nil && ! c .Session .Closed () {
628
- c .Session .Close ()
629
- }
630
658
}
0 commit comments