mysql client pool work for indexer; temporary glue in blobserver

This commit is contained in:
Brett Slatkin 2011-03-05 15:09:36 -08:00
parent 9924b32691
commit e36a7679b2
3 changed files with 60 additions and 7 deletions

View File

@ -28,6 +28,6 @@ func (mi *Indexer) EnumerateBlobs(dest chan *blobref.SizedBlobRef,
limit uint,
waitSeconds int) os.Error {
// TODO: finish
return os.NewError("Fetch isn't supported by the MySQL indexer")
return os.NewError("EnumerateBlobs isn't supported by the MySQL indexer")
}

View File

@ -37,6 +37,49 @@ type Indexer struct {
cachedClients []*mysql.Client
}
func (mi *Indexer) IsAlive() (ok bool, err os.Error) {
var client *mysql.Client
client, err = mi.getConnection()
defer mi.releaseConnection(client)
if err != nil {
return
}
err = client.Query("SELECT 1 + 1")
if err != nil {
return
}
_, err = client.UseResult()
if err != nil {
return
}
client.FreeResult()
return true, nil
}
// Get a free cached connection or allocate a new one.
func (mi *Indexer) getConnection() (client *mysql.Client, err os.Error) {
mi.clientLock.Lock()
if len(mi.cachedClients) > 0 {
defer mi.clientLock.Unlock()
client = mi.cachedClients[len(mi.cachedClients)-1]
mi.cachedClients = mi.cachedClients[:len(mi.cachedClients)-1]
// TODO: Outside the mutex, double check that the client is still good.
return
}
mi.clientLock.Unlock()
client, err = mysql.DialTCP(mi.Host, mi.User, mi.Password, mi.Database)
return
}
// Release a client to the cached client pool.
func (mi *Indexer) releaseConnection(client *mysql.Client) {
mi.clientLock.Lock()
defer mi.clientLock.Unlock()
mi.cachedClients = append(mi.cachedClients, client)
}
func (mi *Indexer) GetBlobHub(partition blobserver.Partition) blobserver.BlobHub {
mi.hubLock.Lock()
defer mi.hubLock.Unlock()

View File

@ -20,8 +20,11 @@ import (
"os"
)
var flagStorageRoot *string = flag.String("root", "/tmp/camliroot", "Root directory to store files")
var flagRequestLog *bool = flag.Bool("reqlog", false, "Log incoming requests")
var flagStorageRoot = flag.String("root", "/tmp/camliroot", "Root directory to store files")
var flagRequestLog = flag.Bool("reqlog", false, "Log incoming requests")
// TODO: Temporary
var flagDevMySql = flag.Bool("devmysqlindexer", false, "Temporary option to enable MySQL indexer on /indexer")
var storage blobserver.Storage
var indexerStorage blobserver.Storage
@ -178,13 +181,20 @@ func main() {
ws.HandleFunc("/camli/", handleCamli)
// TODO: temporary
if true {
indexerStorage = &mysqlindexer.Indexer{}
if *flagDevMySql {
myIndexer := &mysqlindexer.Indexer{
Host: "localhost",
User: "root",
Password: "root",
Database: "camlistore",
}
if ok, err := myIndexer.IsAlive(); !ok {
log.Fatalf("Could not connect indexer to MySQL server: %s", err)
}
indexerStorage = myIndexer
ws.HandleFunc("/indexer/", handleIndexRequest)
}
ws.Handle("/js/", http.FileServer("../../clients/js", "/js/"))
ws.Serve()
}