func newPool(s *site, name string) (*pool, query.Error) { clog.To(catalog.CHANNEL, "Created New Pool %s", name) cbpool, err := s.client.GetPool(name) if err != nil { if name == "default" { // if default pool is not available, try reconnecting to the server url := s.URL() client, err := cb.Connect(url) if err != nil { return nil, query.NewError(nil, fmt.Sprintf("Pool %v not found.", name)) } // check if the default pool exists cbpool, err = client.GetPool(name) if err != nil { return nil, query.NewError(nil, fmt.Sprintf("Pool %v not found.", name)) } s.client = client } } rv := pool{ site: s, name: name, cbpool: cbpool, bucketCache: make(map[string]catalog.Bucket), } go keepPoolFresh(&rv) return &rv, nil }
func (p *namespace) refresh(changed bool) { // trigger refresh of this pool logging.Infof("Refreshing pool %s", p.name) newpool, err := p.site.client.GetPool(p.name) if err != nil { var client cb.Client logging.Errorf("Error updating pool name %s: Error %v", p.name, err) url := p.site.URL() /* transport := cbauth.WrapHTTPTransport(cb.HTTPTransport, nil) cb.HTTPClient.Transport = transport */ if p.site.CbAuthInit == true { client, err = cb.ConnectWithAuth(url, cbauth.NewAuthHandler(nil)) } else { client, err = cb.Connect(url) } if err != nil { logging.Errorf("Error connecting to URL %s", url) return } // check if the default pool exists newpool, err = client.GetPool(p.name) if err != nil { logging.Errorf("Retry Failed Error updating pool name %s: Error %v", p.name, err) return } p.site.client = client } p.lock.Lock() defer p.lock.Unlock() for name, ks := range p.keyspaceCache { logging.Infof(" Checking keyspace %s", name) _, err := newpool.GetBucket(name) if err != nil { changed = true ks.(*keyspace).deleted = true logging.Errorf(" Error retrieving bucket %s", name) delete(p.keyspaceCache, name) } // Not deleted. Check if GSI indexer is available if ks.(*keyspace).gsiIndexer == nil { ks.(*keyspace).refreshIndexer(p.site.URL(), p.Name()) } } if changed == true { p.setPool(newpool) } }
// NewSite creates a new Couchbase site for the given url. func NewSite(url string) (catalog.Site, query.Error) { clog.To(catalog.CHANNEL, "Created New Site %s", url) client, err := cb.Connect(url) if err != nil { return nil, query.NewError(err, "") } return &site{ client: client, poolCache: make(map[string]catalog.Pool), }, nil }
func (p *pool) refresh() { // trigger refresh of this pool clog.To(catalog.CHANNEL, "Refreshing Pool %s", p.name) newpool, err := p.site.client.GetPool(p.name) if err != nil { clog.Warnf("Error updating pool name %s: Error %v", p.name, err) url := p.site.URL() client, err := cb.Connect(url) if err != nil { clog.Warnf("Error connecting to URL %s", url) } // check if the default pool exists newpool, err = client.GetPool(p.name) if err != nil { clog.Warnf("Retry Failed Error updating pool name %s: Error %v", p.name, err) } p.site.client = client return } p.cbpool = newpool }
func loadNamespace(s *site, name string) (*namespace, errors.Error) { cbpool, err := s.client.GetPool(name) if err != nil { if name == "default" { // if default pool is not available, try reconnecting to the server url := s.URL() var client cb.Client if s.CbAuthInit == true { client, err = cb.ConnectWithAuth(url, cbauth.NewAuthHandler(nil)) } else { client, err = cb.Connect(url) } if err != nil { return nil, errors.NewCbNamespaceNotFoundError(err, "Namespace "+name) } // check if the default pool exists cbpool, err = client.GetPool(name) if err != nil { return nil, errors.NewCbNamespaceNotFoundError(err, "Namespace "+name) } s.client = client } } rv := namespace{ site: s, name: name, cbNamespace: cbpool, keyspaceCache: make(map[string]datastore.Keyspace), } go keepPoolFresh(&rv) return &rv, nil }
// NewSite creates a new Couchbase site for the given url. func NewDatastore(u string) (s datastore.Datastore, e errors.Error) { var client cb.Client var cbAuthInit bool // try and initialize cbauth c, err := initCbAuth(u) if err != nil { logging.Errorf(" Unable to initialize cbauth. Error %v", err) url, err := url.Parse(u) if err != nil { return nil, errors.NewCbUrlParseError(err, "url "+u) } if url.User != nil { password, _ := url.User.Password() if password == "" { logging.Errorf("No password found in url %s", u) } // intialize cb_auth variables manually logging.Infof(" Trying to init cbauth with credentials %s %s %s", url.Host, url.User.Username(), password) set, err := cbauth.InternalRetryDefaultInit(url.Host, url.User.Username(), password) if set == false || err != nil { logging.Errorf(" Unable to initialize cbauth variables. Error %v", err) } else { c, err = initCbAuth("http://" + url.Host) if err != nil { logging.Errorf("Unable to initliaze cbauth. Error %v", err) } else { client = *c cbAuthInit = true } } } } else { client = *c cbAuthInit = true } if cbAuthInit == false { // connect without auth cb.HTTPClient = &http.Client{} client, err = cb.Connect(u) if err != nil { return nil, errors.NewCbConnectionError(err, "url "+u) } } site := &site{ client: client, namespaceCache: make(map[string]*namespace), CbAuthInit: cbAuthInit, } // initialize the default pool. // TODO can couchbase server contain more than one pool ? defaultPool, Err := loadNamespace(site, "default") if Err != nil { logging.Errorf("Cannot connect to default pool") return nil, Err } site.namespaceCache["default"] = defaultPool logging.Infof("New site created with url %s", u) return site, nil }