Exemple #1
0
func (dcmd *delCommand) Apply(server raft.Server) (interface{}, error) {
	if server.Name() == server.Leader() {
		return nil, nil
	}
	kvs := server.Context().(KVstore)
	err := kvs.Delete(dcmd.Key)
	return nil, err
}
Exemple #2
0
func (c *AddCallbackCommand) Apply(server raft.Server) (interface{}, error) {
	reg := server.Context().(registry.Registry)
	err := reg.AddCallback(c.Service, c.Callback)
	if err == nil {
		log.Println("Added Callback:", c.Service, c.Callback)
	}
	return c.Service, err
}
Exemple #3
0
// Apply implements goraft Command interface's Apply function
// It puts a key-value pair in KVstore
func (pcmd *putCommand) Apply(server raft.Server) (interface{}, error) {
	if server.Name() == server.Leader() {
		return nil, nil
	}
	kvs := server.Context().(KVstore)
	err := kvs.Put(pcmd.Key, pcmd.Val)
	return nil, err
}
//???
func (c *MaxVolumeIdCommand) Apply(server raft.Server) (interface{}, error) {
	topo := server.Context().(*Topology)
	before := topo.GetMaxVolumeId()
	topo.UpAdjustMaxVolumeId(c.MaxVolumeId)

	glog.V(4).Infoln("max volume id", before, "==>", topo.GetMaxVolumeId())

	return nil, nil
}
Exemple #5
0
// Updates TTL in registry
func (c *UpdateTTLCommand) Apply(server raft.Server) (interface{}, error) {
	reg := server.Context().(registry.Registry)
	err := reg.UpdateTTL(c.UUID, c.TTL, c.Expires)

	if err == nil {
		log.Println("Updated Service TTL:", c.UUID, c.TTL)
	}

	return c.UUID, err
}
Exemple #6
0
// Removes service from the registry
func (c *RemoveServiceCommand) Apply(server raft.Server) (interface{}, error) {

	reg := server.Context().(registry.Registry)
	err := reg.RemoveUUID(c.UUID)

	if err == nil {
		log.Println("Removed Service:", c.UUID)
	}

	return c.UUID, err
}
// Writes a value to a key.
func (c *QueryCommand) Apply(server raft.Server) (interface{}, error) {
	db := server.Context().(*sql.SQL)

	output, err := db.CacheExec(c.Create, c.Name, c.FriendCount, c.FavoriteWord)
	if err != nil {
		return nil, err
	}

	formatted := fmt.Sprintf("SequenceNumber: %d\n%s", output.SequenceNumber, output.Output)

	return []byte(formatted), nil
}
Exemple #8
0
func (c *CreateShardsCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*cluster.ClusterConfiguration)
	createdShards, err := config.AddShards(c.Shards)
	if err != nil {
		return nil, err
	}
	createdShardData := make([]*cluster.NewShardData, 0)
	for _, s := range createdShards {
		createdShardData = append(createdShardData, s.ToNewShardData())
	}
	return createdShardData, nil
}
Exemple #9
0
func (c *SaveSubscriptionCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*cluster.ClusterConfiguration)
	createdSubscription, err := config.AddSubscription(c.Subscription)
	fmt.Println("we are here in command.go's Apply function")
	if err != nil {
		return nil, err
	}
	//createdSubscriptionData := make(*cluster.Subscription, 0)
	//for _, s := range createdSubscription {
	//createdSubscriptionData = append(createdSubscriptionData, s.ToNewSubscriptionData())
	//}
	//return createdSubscriptionData, nil
	return createdSubscription, nil
	return nil, nil
}
Exemple #10
0
// Execute an SQL statement
func (c *WriteCommand) Apply(server raft.Server) (interface{}, error) {
	sql := server.Context().(*sql.SQL)
	job := sql.Execute(c.Query)

	go func() {
		output := []byte(<-job.OutChan)
		seq := job.Seq

		formatted := fmt.Sprintf("SequenceNumber: %d\n%s",
			seq, output)

		sql.Respond(c.Id, []byte(formatted))
	}()

	return []byte(""), nil
}
Exemple #11
0
func (c *InfluxForceLeaveCommand) Apply(server raft.Server) (interface{}, error) {
	clusterConfig := server.Context().(*cluster.ClusterConfiguration)
	s := clusterConfig.GetServerById(&c.Id)

	if s == nil {
		return nil, nil
	}

	if err := server.RemovePeer(s.RaftName); err != nil {
		log.Warn("Cannot remove peer: %s", err)
	}

	if err := clusterConfig.RemoveServer(s); err != nil {
		log.Warn("Cannot remove peer from cluster config: %s", err)
	}
	server.FlushCommitIndex()
	return nil, nil
}
Exemple #12
0
// Writes a value to a key.
func (c *BatchCommand) Apply(server raft.Server) (interface{}, error) {
	//log.Printf(("Applying Batch Size: %v", len(c.Queries))
	db := server.Context().(*DB)

	for _, output := range db.ExecuteBatch(c.Queries) {
		db.notify <- output
	}

	// for _, query := range c.Queries {
	// 	output, err := db.Execute(*query)

	// 	if err != nil {
	// 		log.Fatal("Error applying command")
	// 		return nil, err
	// 	}

	// 	db.notify <- output

	// }
	return nil, nil
}
Exemple #13
0
func (c *InfluxChangeConnectionStringCommand) Apply(server raft.Server) (interface{}, error) {
	if c.Name == server.Name() {
		return nil, nil
	}

	server.RemovePeer(c.Name)
	server.AddPeer(c.Name, c.ConnectionString)

	clusterConfig := server.Context().(*cluster.ClusterConfiguration)

	newServer := clusterConfig.GetServerByRaftName(c.Name)
	// it's a new server the cluster has never seen, make it a potential
	if newServer == nil {
		return nil, fmt.Errorf("Server %s doesn't exist", c.Name)
	}

	newServer.RaftConnectionString = c.ConnectionString
	newServer.ProtobufConnectionString = c.ProtobufConnectionString
	server.Context().(*cluster.ClusterConfiguration).ChangeProtobufConnectionString(newServer)
	server.FlushCommitIndex()
	return nil, nil
}
Exemple #14
0
// Writes a value to a key.
func (c *SqlCommand) Apply(server raft.Server) (interface{}, error) {
	db := server.Context().(*sql.SQL)
	output, err := db.Execute("tehrafts", inflate(c.Query))

	if err != nil {
		var msg string
		if output != nil && len(output.Stderr) > 0 {
			template := `Error executing %#v (%s)

SQLite error: %s`
			msg = fmt.Sprintf(template, c.Query, err.Error(), util.FmtOutput(output.Stderr))
		} else {
			msg = err.Error()
		}

		return nil, errors.New(msg)
	}

	formatted := fmt.Sprintf("SequenceNumber: %d\n%s",
		output.SequenceNumber, output.Stdout)
	return []byte(formatted), nil
}
Exemple #15
0
func (c *InfluxJoinCommand) Apply(server raft.Server) (interface{}, error) {
	err := server.AddPeer(c.Name, c.ConnectionString)
	if err != nil {
		return nil, err
	}

	clusterConfig := server.Context().(*cluster.ClusterConfiguration)

	newServer := clusterConfig.GetServerByRaftName(c.Name)
	// it's a new server the cluster has never seen, make it a potential
	if newServer != nil {
		return nil, fmt.Errorf("Server %s already exist", c.Name)
	}

	log.Info("Adding new server to the cluster config %s", c.Name)
	clusterServer := cluster.NewClusterServer(c.Name,
		c.ConnectionString,
		c.ProtobufConnectionString,
		nil,
		clusterConfig.GetLocalConfiguration())
	clusterConfig.AddPotentialServer(clusterServer)
	return nil, nil
}
Exemple #16
0
func (c *SaveDbUserCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*cluster.ClusterConfiguration)
	config.SaveDbUser(c.User)
	log.Debug("(raft:%s) Created user %s:%s", server.Name(), c.User.Db, c.User.Name)
	return nil, nil
}
Exemple #17
0
func (c *WriteCommand) Apply(server raft.Server) (interface{}, error) {
	db := server.Context().(*DB)
	db.Put(c.Key, c.Value)
	return nil, nil
}
Exemple #18
0
func (c *DeleteContinuousQueryCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*cluster.ClusterConfiguration)
	err := config.DeleteContinuousQuery(c.Database, c.Id)
	return nil, err
}
Exemple #19
0
func (c *SetContinuousQueryTimestampCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*cluster.ClusterConfiguration)
	err := config.SetContinuousQueryTimestamp(c.Timestamp)
	return nil, err
}
Exemple #20
0
func (c *DropShardCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*cluster.ClusterConfiguration)
	err := config.DropShard(c.ShardId, c.ServerIds)
	return nil, err
}
Exemple #21
0
func (c *UpdateServerStateCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*ClusterConfiguration)
	err := config.UpdateServerState(c.ServerId, c.State)
	return nil, err
}
Exemple #22
0
func (c *AddPotentialServerCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*cluster.ClusterConfiguration)
	config.AddPotentialServer(c.Server)
	return nil, nil
}
Exemple #23
0
func (c *SaveClusterAdminCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*cluster.ClusterConfiguration)
	config.SaveClusterAdmin(c.User)
	return nil, nil
}
Exemple #24
0
func (c *ChangeDbUserPermissions) Apply(server raft.Server) (interface{}, error) {
	log.Debug("(raft:%s) changing db user permissions for %s:%s", server.Name(), c.Database, c.Username)
	config := server.Context().(*cluster.ClusterConfiguration)
	return nil, config.ChangeDbUserPermissions(c.Database, c.Username, c.ReadPermissions, c.WritePermissions)
}
Exemple #25
0
func (c *ChangeDbUserPassword) Apply(server raft.Server) (interface{}, error) {
	log.Debug("(raft:%s) changing db user password for %s:%s", server.Name(), c.Database, c.Username)
	config := server.Context().(*cluster.ClusterConfiguration)
	return nil, config.ChangeDbUserPassword(c.Database, c.Username, c.Hash)
}
Exemple #26
0
// Writes a value to a key.
func (c *SetCommand) Apply(server raft.Server) (interface{}, error) {
	fs := server.Context().(*db.Fs)

	return fs.Apply(c.Key, c.Value, c.Verb, c.Oper)
}
Exemple #27
0
func (c *CreateDatabaseCommand) Apply(server raft.Server) (interface{}, error) {
	config := server.Context().(*cluster.ClusterConfiguration)
	err := config.CreateDatabase(c.Name)
	return nil, err
}
// Writes a value to a key.
func (c *RemoveTmpDataCommand) Apply(server raft.Server) (interface{}, error) {
	fs := server.Context().(*db.Fs)
	//log.Printf("RemoveTempDataIp: %v\n", c.Key)
	fs.RemoveTempData(c.Key)
	return nil, nil
}