Skip to content

Commit 4c264e0

Browse files
committed
Create, show and delete reservation
1 parent f8a1d0f commit 4c264e0

File tree

4 files changed

+169
-53
lines changed

4 files changed

+169
-53
lines changed

internal/ccontrol/CmdArgParser.go

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ var (
9797
}
9898
},
9999
}
100+
showReservationsCmd = &cobra.Command{
101+
Use: "reservation [flags] [reservation_name]",
102+
Short: "Display details of the reservations, default is all",
103+
Long: "",
104+
Args: cobra.MaximumNArgs(1),
105+
Run: func(cmd *cobra.Command, args []string) {
106+
if len(args) == 0 {
107+
FlagReservationName = ""
108+
FlagQueryAll = true
109+
} else {
110+
FlagReservationName = args[0]
111+
FlagQueryAll = false
112+
}
113+
if err := ShowReservations(FlagReservationName, FlagQueryAll); err != util.ErrorSuccess {
114+
os.Exit(err)
115+
}
116+
},
117+
}
100118
showJobCmd = &cobra.Command{
101119
Use: "job [flags] [job_id,...]",
102120
Short: "Display details of the jobs, default is all",
@@ -116,17 +134,6 @@ var (
116134
}
117135
},
118136
}
119-
// showReservationsCmd = &cobra.Command{
120-
// Use: "reservation",
121-
// Short: "Display details of the reservations",
122-
// Long: "",
123-
// Args: cobra.ExactArgs(0),
124-
// Run: func(cmd *cobra.Command, args []string) {
125-
// if err := ShowReservations(); err != util.ErrorSuccess {
126-
// os.Exit(err)
127-
// }
128-
// },
129-
// }
130137
showConfigCmd = &cobra.Command{
131138
Use: "config",
132139
Short: "Display the configuration file in key-value format",
@@ -238,6 +245,22 @@ var (
238245
}
239246
},
240247
}
248+
deleteCmd = &cobra.Command{
249+
Use: "delete",
250+
Short: "Delete the specified entity",
251+
Long: "",
252+
}
253+
deleteReservationCmd = &cobra.Command{
254+
Use: "reservation reservation_name",
255+
Short: "Delete the specified reservation",
256+
Long: "",
257+
Args: cobra.ExactArgs(1),
258+
Run: func(cmd *cobra.Command, args []string) {
259+
if err := DeleteReservation(args[0]); err != util.ErrorSuccess {
260+
os.Exit(err)
261+
}
262+
},
263+
}
241264
)
242265

243266
// ParseCmdArgs executes the root command.
@@ -259,7 +282,7 @@ func init() {
259282
showCmd.AddCommand(showPartitionCmd)
260283
showCmd.AddCommand(showJobCmd)
261284
showCmd.AddCommand(showConfigCmd)
262-
// showCmd.AddCommand(showReservationsCmd)
285+
showCmd.AddCommand(showReservationsCmd)
263286
}
264287

265288
RootCmd.AddCommand(updateCmd)
@@ -298,7 +321,30 @@ func init() {
298321
createReservationCmd.Flags().StringVarP(&FlagReservationName, "name", "n", "", "Specify the name of the reservation")
299322
createReservationCmd.Flags().StringVarP(&FlagStartTime, "start-time", "s", "", "Specify the start time of the reservation")
300323
createReservationCmd.Flags().StringVarP(&FlagDuration, "duration", "d", "", "Specify the duration of the reservation")
324+
createReservationCmd.Flags().StringVarP(&FlagPartitionName, "partition", "p", "", "Specify the partition of the reservation")
301325
createReservationCmd.Flags().StringVarP(&FlagNodes, "nodes", "N", "", "Specify the nodes of the reservation")
326+
327+
err := createReservationCmd.MarkFlagRequired("name")
328+
if err != nil {
329+
return
330+
}
331+
err = createReservationCmd.MarkFlagRequired("start-time")
332+
if err != nil {
333+
return
334+
}
335+
err = createReservationCmd.MarkFlagRequired("duration")
336+
if err != nil {
337+
return
338+
}
339+
err = createReservationCmd.MarkFlagRequired("nodes")
340+
if err != nil {
341+
return
342+
}
302343
}
303344
}
345+
346+
RootCmd.AddCommand(deleteCmd)
347+
{
348+
deleteCmd.AddCommand(deleteReservationCmd)
349+
}
304350
}

internal/ccontrol/ccontrol.go

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"strings"
3333
"time"
3434

35-
"google.golang.org/protobuf/types/known/timestamppb"
35+
"google.golang.org/protobuf/types/known/durationpb"
3636
"gopkg.in/yaml.v3"
3737

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

202-
if len(reply.PartitionInfo) == 0 {
203+
if len(reply.PartitionInfoList) == 0 {
203204
if queryAll {
204205
fmt.Println("No partition is available.")
205206
} else {
206207
fmt.Printf("Partition %s not found.\n", partitionName)
208+
return util.ErrorBackend
207209
}
208210
} else {
209-
for _, partitionInfo := range reply.PartitionInfo {
211+
for _, partitionInfo := range reply.PartitionInfoList {
210212
fmt.Printf("PartitionName=%v State=%v\n"+
211213
"\tTotalNodes=%d AliveNodes=%d\n"+
212214
"\tTotalCPU=%.2f AvailCPU=%.2f AllocCPU=%.2f\n"+
@@ -230,6 +232,51 @@ func ShowPartitions(partitionName string, queryAll bool) util.CraneCmdError {
230232
return util.ErrorSuccess
231233
}
232234

235+
func ShowReservations(reservationName string, queryAll bool) util.CraneCmdError {
236+
req := &protos.QueryReservationInfoRequest{ReservationName: reservationName}
237+
reply, err := stub.QueryReservationInfo(context.Background(), req)
238+
if err != nil {
239+
util.GrpcErrorPrintf(err, "Failed to show reservations")
240+
return util.ErrorNetwork
241+
}
242+
243+
if FlagJson {
244+
fmt.Println(util.FmtJson.FormatReply(reply))
245+
return util.ErrorSuccess
246+
}
247+
248+
if len(reply.ReservationInfoList) == 0 {
249+
if queryAll {
250+
fmt.Println("No reservation is available.")
251+
} else {
252+
fmt.Printf("Reservation %s not found.\n", reservationName)
253+
return util.ErrorBackend
254+
}
255+
} else {
256+
for _, reservationInfo := range reply.ReservationInfoList {
257+
fmt.Printf("ReservationName=%v StartTime=%v Duration=%v\n"+
258+
"\tPartition=%v CranedRegex=%v\n"+
259+
"\tTotalCPU=%.2f AvailCPU=%.2f AllocCPU=%.2f\n"+
260+
"\tTotalMem=%s AvailMem=%s AllocMem=%s\n"+
261+
"\tTotalGres=%s AvailGres=%s AllocGres=%s\n\n",
262+
reservationInfo.ReservationName, reservationInfo.StartTime.AsTime().In(time.Local).Format("2006-01-02 15:04:05"),
263+
reservationInfo.Duration.AsDuration().String(),
264+
reservationInfo.Partition, reservationInfo.CranedRegex,
265+
math.Abs(reservationInfo.ResTotal.AllocatableRes.CpuCoreLimit),
266+
math.Abs(reservationInfo.ResAvail.AllocatableRes.CpuCoreLimit),
267+
math.Abs(reservationInfo.ResAlloc.AllocatableRes.CpuCoreLimit),
268+
formatMemToMB(reservationInfo.ResTotal.AllocatableRes.MemoryLimitBytes),
269+
formatMemToMB(reservationInfo.ResAvail.AllocatableRes.MemoryLimitBytes),
270+
formatMemToMB(reservationInfo.ResAlloc.AllocatableRes.MemoryLimitBytes),
271+
formatDeviceMap(reservationInfo.ResTotal.GetDeviceMap()),
272+
formatDeviceMap(reservationInfo.ResAvail.GetDeviceMap()),
273+
formatDeviceMap(reservationInfo.ResAlloc.GetDeviceMap()),
274+
)
275+
}
276+
}
277+
return util.ErrorSuccess
278+
}
279+
233280
func ShowJobs(jobIds string, queryAll bool) util.CraneCmdError {
234281
var req *protos.QueryTasksInfoRequest
235282
var jobIdList []uint32
@@ -664,19 +711,19 @@ func CreateReservation() util.CraneCmdError {
664711
log.Errorln(err)
665712
return util.ErrorCmdArg
666713
}
667-
668-
reservation_info := &protos.ReservationInfo{}
669-
reservation_info.ReservationName = FlagReservationName
670-
reservation_info.StartTime = timestamppb.New(start_time)
671-
ok := util.ParseDuration(FlagDuration, reservation_info.Duration)
672-
if !ok {
714+
duration := durationpb.New(time.Duration(0))
715+
ok := util.ParseDuration(FlagDuration, duration)
716+
if !ok || duration.AsDuration() < 0 {
673717
log.Errorln("Invalid duration specified.")
674718
return util.ErrorCmdArg
675719
}
676-
reservation_info.CranedRegex = FlagNodes
677720

678721
req := &protos.CreateReservationRequest{
679-
ReservationInfo: reservation_info,
722+
ReservationName: FlagReservationName,
723+
StartTimeSeconds: start_time.Unix(),
724+
DurationSeconds: duration.GetSeconds(),
725+
Partition: FlagPartitionName,
726+
CranedRegex: FlagNodes,
680727
}
681728

682729
reply, err := stub.CreateReservation(context.Background(), req)
@@ -692,4 +739,21 @@ func CreateReservation() util.CraneCmdError {
692739
return util.ErrorBackend
693740
}
694741
return util.ErrorSuccess
695-
}
742+
}
743+
744+
func DeleteReservation(ReservationName string) util.CraneCmdError {
745+
req := &protos.DeleteReservationRequest{ReservationName: ReservationName}
746+
reply, err := stub.DeleteReservation(context.Background(), req)
747+
if err != nil {
748+
util.GrpcErrorPrintf(err, "Failed to delete reservation")
749+
return util.ErrorNetwork
750+
}
751+
752+
if reply.GetOk() {
753+
fmt.Printf("Reservation %s deleted successfully.\n", ReservationName)
754+
} else {
755+
log.Errorf("Failed to delete reservation: %s.\n", reply.GetReason())
756+
return util.ErrorBackend
757+
}
758+
return util.ErrorSuccess
759+
}

protos/Crane.proto

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,15 @@ message QueryPartitionInfoRequest {
191191
}
192192

193193
message QueryPartitionInfoReply {
194-
repeated PartitionInfo partition_info = 1;
194+
repeated PartitionInfo partition_info_list = 1;
195+
}
196+
197+
message QueryReservationInfoRequest {
198+
string reservation_name = 1;
199+
}
200+
201+
message QueryReservationInfoReply {
202+
repeated ReservationInfo reservation_info_list = 3;
195203
}
196204

197205
message ModifyTaskRequest {
@@ -394,33 +402,26 @@ message QueryTasksInfoReply{
394402
}
395403

396404
message CreateReservationRequest {
397-
ReservationInfo reservation_info = 1;
405+
string reservation_name = 1;
406+
int64 start_time_seconds = 2;
407+
int64 duration_seconds = 3;
408+
string partition = 4;
409+
string craned_regex = 5;
398410
}
399411

400412
message CreateReservationReply {
401413
bool ok = 1;
402414
string reason = 2;
403415
}
404416

405-
// message DeleteReservationRequest {
406-
// string reservation_name = 1;
407-
// }
408-
409-
// message DeleteReservationReply {
410-
// bool ok = 1;
411-
// string reason = 2;
412-
// }
413-
414-
// message QueryReservationRequest {
415-
// repeated string filter_reservation_name = 1;
416-
// uint32 num_limit = 3;
417-
// }
417+
message DeleteReservationRequest {
418+
string reservation_name = 1;
419+
}
418420

419-
// message QueryReservationReply {
420-
// bool ok = 1;
421-
// string reason = 2;
422-
// repeated ReservationInfo reservation_info_list = 3;
423-
// }
421+
message DeleteReservationReply {
422+
bool ok = 1;
423+
string reason = 2;
424+
}
424425

425426
message StreamCallocRequest {
426427
enum CallocRequestType {
@@ -740,6 +741,7 @@ service CraneCtld {
740741
/* PRCs called from ccontrol */
741742
rpc QueryCranedInfo(QueryCranedInfoRequest) returns (QueryCranedInfoReply);
742743
rpc QueryPartitionInfo(QueryPartitionInfoRequest) returns (QueryPartitionInfoReply);
744+
rpc QueryReservationInfo(QueryReservationInfoRequest) returns (QueryReservationInfoReply);
743745
rpc ModifyTask(ModifyTaskRequest) returns (ModifyTaskReply);
744746
rpc ModifyNode(ModifyCranedStateRequest) returns (ModifyCranedStateReply);
745747

@@ -760,8 +762,7 @@ service CraneCtld {
760762
/* common RPCs */
761763
rpc QueryTasksInfo(QueryTasksInfoRequest) returns (QueryTasksInfoReply);
762764
rpc CreateReservation(CreateReservationRequest) returns (CreateReservationReply);
763-
// rpc DeleteReservation(DeleteReservationRequest) returns (DeleteReservationReply);
764-
// rpc QueryReservation(QueryReservationRequest) returns (QueryReservationReply);
765+
rpc DeleteReservation(DeleteReservationRequest) returns (DeleteReservationReply);
765766
}
766767

767768
service Craned {

protos/PublicDefs.proto

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,18 @@ message CranedInfo {
303303
google.protobuf.Timestamp last_busy_time = 16;
304304
}
305305

306+
message ReservationInfo {
307+
string reservation_name = 1;
308+
google.protobuf.Timestamp start_time = 2;
309+
google.protobuf.Duration duration = 3;
310+
string partition = 4;
311+
string craned_regex = 5;
312+
313+
ResourceView res_total = 6;
314+
ResourceView res_avail = 7;
315+
ResourceView res_alloc = 8;
316+
}
317+
306318
message TrimmedPartitionInfo {
307319
message TrimmedCranedInfo {
308320
CranedResourceState resource_state = 1;
@@ -392,11 +404,4 @@ message CranedRemoteMeta {
392404
string craned_version = 3;
393405
google.protobuf.Timestamp craned_start_time = 4;
394406
google.protobuf.Timestamp system_boot_time = 5;
395-
}
396-
397-
message ReservationInfo {
398-
string reservation_name = 1;
399-
google.protobuf.Timestamp start_time = 2;
400-
google.protobuf.Duration duration = 3;
401-
string craned_regex = 4;
402407
}

0 commit comments

Comments
 (0)