Skip to content

Commit

Permalink
Create, show and delete reservation
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessOIer committed Nov 21, 2024
1 parent 4ba809e commit 1e0e34c
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 53 deletions.
70 changes: 58 additions & 12 deletions internal/ccontrol/CmdArgParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ var (
}
},
}
showReservationsCmd = &cobra.Command{
Use: "reservation [flags] [reservation_name]",
Short: "Display details of the reservations, default is all",
Long: "",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
FlagReservationName = ""
FlagQueryAll = true
} else {
FlagReservationName = args[0]
FlagQueryAll = false
}
if err := ShowReservations(FlagReservationName, FlagQueryAll); err != util.ErrorSuccess {
os.Exit(err)
}
},
}
showJobCmd = &cobra.Command{
Use: "job [flags] [job_id,...]",
Short: "Display details of the jobs, default is all",
Expand All @@ -116,17 +134,6 @@ var (
}
},
}
// showReservationsCmd = &cobra.Command{
// Use: "reservation",
// Short: "Display details of the reservations",
// Long: "",
// Args: cobra.ExactArgs(0),
// Run: func(cmd *cobra.Command, args []string) {
// if err := ShowReservations(); err != util.ErrorSuccess {
// os.Exit(err)
// }
// },
// }
showConfigCmd = &cobra.Command{
Use: "config",
Short: "Display the configuration file in key-value format",
Expand Down Expand Up @@ -238,6 +245,22 @@ var (
}
},
}
deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete the specified entity",
Long: "",
}
deleteReservationCmd = &cobra.Command{
Use: "reservation reservation_name",
Short: "Delete the specified reservation",
Long: "",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := DeleteReservation(args[0]); err != util.ErrorSuccess {
os.Exit(err)
}
},
}
)

// ParseCmdArgs executes the root command.
Expand All @@ -259,7 +282,7 @@ func init() {
showCmd.AddCommand(showPartitionCmd)
showCmd.AddCommand(showJobCmd)
showCmd.AddCommand(showConfigCmd)
// showCmd.AddCommand(showReservationsCmd)
showCmd.AddCommand(showReservationsCmd)
}

RootCmd.AddCommand(updateCmd)
Expand Down Expand Up @@ -298,7 +321,30 @@ func init() {
createReservationCmd.Flags().StringVarP(&FlagReservationName, "name", "n", "", "Specify the name of the reservation")
createReservationCmd.Flags().StringVarP(&FlagStartTime, "start-time", "s", "", "Specify the start time of the reservation")
createReservationCmd.Flags().StringVarP(&FlagDuration, "duration", "d", "", "Specify the duration of the reservation")
createReservationCmd.Flags().StringVarP(&FlagPartitionName, "partition", "p", "", "Specify the partition of the reservation")
createReservationCmd.Flags().StringVarP(&FlagNodes, "nodes", "N", "", "Specify the nodes of the reservation")

err := createReservationCmd.MarkFlagRequired("name")
if err != nil {
return
}
err = createReservationCmd.MarkFlagRequired("start-time")
if err != nil {
return
}
err = createReservationCmd.MarkFlagRequired("duration")
if err != nil {
return
}
err = createReservationCmd.MarkFlagRequired("nodes")
if err != nil {
return
}
}
}

RootCmd.AddCommand(deleteCmd)
{
deleteCmd.AddCommand(deleteReservationCmd)
}
}
88 changes: 76 additions & 12 deletions internal/ccontrol/ccontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"strings"
"time"

"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/durationpb"
"gopkg.in/yaml.v3"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -122,6 +122,7 @@ func ShowNodes(nodeName string, queryAll bool) util.CraneCmdError {
fmt.Println("No node is available.")
} else {
fmt.Printf("Node %s not found.\n", nodeName)
return util.ErrorBackend
}
} else {
for _, nodeInfo := range reply.CranedInfoList {
Expand Down Expand Up @@ -199,14 +200,15 @@ func ShowPartitions(partitionName string, queryAll bool) util.CraneCmdError {
return util.ErrorSuccess
}

if len(reply.PartitionInfo) == 0 {
if len(reply.PartitionInfoList) == 0 {
if queryAll {
fmt.Println("No partition is available.")
} else {
fmt.Printf("Partition %s not found.\n", partitionName)
return util.ErrorBackend
}
} else {
for _, partitionInfo := range reply.PartitionInfo {
for _, partitionInfo := range reply.PartitionInfoList {
fmt.Printf("PartitionName=%v State=%v\n"+
"\tTotalNodes=%d AliveNodes=%d\n"+
"\tTotalCPU=%.2f AvailCPU=%.2f AllocCPU=%.2f\n"+
Expand All @@ -230,6 +232,51 @@ func ShowPartitions(partitionName string, queryAll bool) util.CraneCmdError {
return util.ErrorSuccess
}

func ShowReservations(reservationName string, queryAll bool) util.CraneCmdError {
req := &protos.QueryReservationInfoRequest{ReservationName: reservationName}
reply, err := stub.QueryReservationInfo(context.Background(), req)
if err != nil {
util.GrpcErrorPrintf(err, "Failed to show reservations")
return util.ErrorNetwork
}

if FlagJson {
fmt.Println(util.FmtJson.FormatReply(reply))
return util.ErrorSuccess
}

if len(reply.ReservationInfoList) == 0 {
if queryAll {
fmt.Println("No reservation is available.")
} else {
fmt.Printf("Reservation %s not found.\n", reservationName)
return util.ErrorBackend
}
} else {
for _, reservationInfo := range reply.ReservationInfoList {
fmt.Printf("ReservationName=%v StartTime=%v Duration=%v\n"+
"\tPartition=%v CranedRegex=%v\n"+
"\tTotalCPU=%.2f AvailCPU=%.2f AllocCPU=%.2f\n"+
"\tTotalMem=%s AvailMem=%s AllocMem=%s\n"+
"\tTotalGres=%s AvailGres=%s AllocGres=%s\n\n",
reservationInfo.ReservationName, reservationInfo.StartTime.AsTime().In(time.Local).Format("2006-01-02 15:04:05"),
reservationInfo.Duration.AsDuration().String(),
reservationInfo.Partition, reservationInfo.CranedRegex,
math.Abs(reservationInfo.ResTotal.AllocatableRes.CpuCoreLimit),
math.Abs(reservationInfo.ResAvail.AllocatableRes.CpuCoreLimit),
math.Abs(reservationInfo.ResAlloc.AllocatableRes.CpuCoreLimit),
formatMemToMB(reservationInfo.ResTotal.AllocatableRes.MemoryLimitBytes),
formatMemToMB(reservationInfo.ResAvail.AllocatableRes.MemoryLimitBytes),
formatMemToMB(reservationInfo.ResAlloc.AllocatableRes.MemoryLimitBytes),
formatDeviceMap(reservationInfo.ResTotal.GetDeviceMap()),
formatDeviceMap(reservationInfo.ResAvail.GetDeviceMap()),
formatDeviceMap(reservationInfo.ResAlloc.GetDeviceMap()),
)
}
}
return util.ErrorSuccess
}

func ShowJobs(jobIds string, queryAll bool) util.CraneCmdError {
var req *protos.QueryTasksInfoRequest
var jobIdList []uint32
Expand Down Expand Up @@ -664,19 +711,19 @@ func CreateReservation() util.CraneCmdError {
log.Errorln(err)
return util.ErrorCmdArg
}

reservation_info := &protos.ReservationInfo{}
reservation_info.ReservationName = FlagReservationName
reservation_info.StartTime = timestamppb.New(start_time)
ok := util.ParseDuration(FlagDuration, reservation_info.Duration)
if !ok {
duration := durationpb.New(time.Duration(0))
ok := util.ParseDuration(FlagDuration, duration)
if !ok || duration.AsDuration() < 0 {
log.Errorln("Invalid duration specified.")
return util.ErrorCmdArg
}
reservation_info.CranedRegex = FlagNodes

req := &protos.CreateReservationRequest{
ReservationInfo: reservation_info,
ReservationName: FlagReservationName,
StartTimeSeconds: start_time.Unix(),
DurationSeconds: duration.GetSeconds(),
Partition: FlagPartitionName,
CranedRegex: FlagNodes,
}

reply, err := stub.CreateReservation(context.Background(), req)
Expand All @@ -692,4 +739,21 @@ func CreateReservation() util.CraneCmdError {
return util.ErrorBackend
}
return util.ErrorSuccess
}
}

func DeleteReservation(ReservationName string) util.CraneCmdError {
req := &protos.DeleteReservationRequest{ReservationName: ReservationName}
reply, err := stub.DeleteReservation(context.Background(), req)
if err != nil {
util.GrpcErrorPrintf(err, "Failed to delete reservation")
return util.ErrorNetwork
}

if reply.GetOk() {
fmt.Printf("Reservation %s deleted successfully.\n", ReservationName)
} else {
log.Errorf("Failed to delete reservation: %s.\n", reply.GetReason())
return util.ErrorBackend
}
return util.ErrorSuccess
}
45 changes: 23 additions & 22 deletions protos/Crane.proto
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,15 @@ message QueryPartitionInfoRequest {
}

message QueryPartitionInfoReply {
repeated PartitionInfo partition_info = 1;
repeated PartitionInfo partition_info_list = 1;
}

message QueryReservationInfoRequest {
string reservation_name = 1;
}

message QueryReservationInfoReply {
repeated ReservationInfo reservation_info_list = 3;
}

message ModifyTaskRequest {
Expand Down Expand Up @@ -450,33 +458,26 @@ message QueryTasksInfoReply{
}

message CreateReservationRequest {
ReservationInfo reservation_info = 1;
string reservation_name = 1;
int64 start_time_seconds = 2;
int64 duration_seconds = 3;
string partition = 4;
string craned_regex = 5;
}

message CreateReservationReply {
bool ok = 1;
string reason = 2;
}

// message DeleteReservationRequest {
// string reservation_name = 1;
// }

// message DeleteReservationReply {
// bool ok = 1;
// string reason = 2;
// }

// message QueryReservationRequest {
// repeated string filter_reservation_name = 1;
// uint32 num_limit = 3;
// }
message DeleteReservationRequest {
string reservation_name = 1;
}

// message QueryReservationReply {
// bool ok = 1;
// string reason = 2;
// repeated ReservationInfo reservation_info_list = 3;
// }
message DeleteReservationReply {
bool ok = 1;
string reason = 2;
}

message StreamCallocRequest {
enum CallocRequestType {
Expand Down Expand Up @@ -796,6 +797,7 @@ service CraneCtld {
/* PRCs called from ccontrol */
rpc QueryCranedInfo(QueryCranedInfoRequest) returns (QueryCranedInfoReply);
rpc QueryPartitionInfo(QueryPartitionInfoRequest) returns (QueryPartitionInfoReply);
rpc QueryReservationInfo(QueryReservationInfoRequest) returns (QueryReservationInfoReply);
rpc ModifyTask(ModifyTaskRequest) returns (ModifyTaskReply);
rpc ModifyNode(ModifyCranedStateRequest) returns (ModifyCranedStateReply);

Expand Down Expand Up @@ -824,8 +826,7 @@ service CraneCtld {
/* common RPCs */
rpc QueryTasksInfo(QueryTasksInfoRequest) returns (QueryTasksInfoReply);
rpc CreateReservation(CreateReservationRequest) returns (CreateReservationReply);
// rpc DeleteReservation(DeleteReservationRequest) returns (DeleteReservationReply);
// rpc QueryReservation(QueryReservationRequest) returns (QueryReservationReply);
rpc DeleteReservation(DeleteReservationRequest) returns (DeleteReservationReply);
}

service Craned {
Expand Down
19 changes: 12 additions & 7 deletions protos/PublicDefs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,18 @@ message CranedInfo {
google.protobuf.Timestamp last_busy_time = 16;
}

message ReservationInfo {
string reservation_name = 1;
google.protobuf.Timestamp start_time = 2;
google.protobuf.Duration duration = 3;
string partition = 4;
string craned_regex = 5;

ResourceView res_total = 6;
ResourceView res_avail = 7;
ResourceView res_alloc = 8;
}

message TrimmedPartitionInfo {
message TrimmedCranedInfo {
CranedResourceState resource_state = 1;
Expand Down Expand Up @@ -478,11 +490,4 @@ message CranedRemoteMeta {
string craned_version = 3;
google.protobuf.Timestamp craned_start_time = 4;
google.protobuf.Timestamp system_boot_time = 5;
}

message ReservationInfo {
string reservation_name = 1;
google.protobuf.Timestamp start_time = 2;
google.protobuf.Duration duration = 3;
string craned_regex = 4;
}

0 comments on commit 1e0e34c

Please sign in to comment.