Added update locked state for buckets

This commit is contained in:
Andrey Terentev 2023-12-04 10:14:35 +07:00
parent 328f2d5011
commit 355983b764
5 changed files with 94 additions and 7 deletions

View File

@ -7,6 +7,8 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import ru.dragonestia.loadbalancer.web.model.type.SlotLimit;
import java.net.URI;
@Getter
public class Bucket {
@ -57,4 +59,8 @@ public class Bucket {
}
return false;
}
public URI createApiURI() {
return URI.create("/nodes/" + nodeIdentifier + "/buckets/" + identifier);
}
}

View File

@ -2,9 +2,13 @@ package ru.dragonestia.loadbalancer.web.page;
import com.vaadin.flow.component.Html;
import com.vaadin.flow.component.Unit;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.Hr;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.NotificationVariant;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
@ -21,16 +25,18 @@ import ru.dragonestia.loadbalancer.web.repository.BucketRepository;
import ru.dragonestia.loadbalancer.web.repository.NodeRepository;
@Route("/nodes/:nodeId/buckets/:bucketId")
public class BucketsPage extends VerticalLayout implements BeforeEnterObserver {
public class BucketDetailsPage extends VerticalLayout implements BeforeEnterObserver {
private final NodeRepository nodeRepository;
private final BucketRepository bucketRepository;
private Node node;
private Bucket bucket;
private AddUsers addUsers;
private Button lockBucketButton;
private VerticalLayout bucketInfo;
@Autowired
public BucketsPage(NodeRepository nodeRepository, BucketRepository bucketRepository) {
public BucketDetailsPage(NodeRepository nodeRepository, BucketRepository bucketRepository) {
this.nodeRepository = nodeRepository;
this.bucketRepository = bucketRepository;
}
@ -87,11 +93,21 @@ public class BucketsPage extends VerticalLayout implements BeforeEnterObserver {
add(new H2("Users"));
}
private void updateBucketInfo() {
bucketInfo.removeAll();
bucketInfo.add(new Html("<span>Node identifier: <b>" + bucket.getNodeIdentifier() + "</b></span>"));
bucketInfo.add(new Html("<span>Bucket identifier: <b>" + bucket.getIdentifier() + "</b></span>"));
bucketInfo.add(new Html("<span>Slots: <b>" + (bucket.getSlots().isUnlimited()? "Unlimited" : bucket.getSlots().slots()) + "</b></span>"));
bucketInfo.add(new Html("<span>Locked: <b>" + (bucket.isLocked()? "Yes" : "No") + "</b></span>"));
}
private void printBucketDetails() {
add(new Html("<span>Node identifier: <b>" + bucket.getNodeIdentifier() + "</b></span>"));
add(new Html("<span>Bucket identifier: <b>" + bucket.getIdentifier() + "</b></span>"));
add(new Html("<span>Slots: <b>" + (bucket.getSlots().isUnlimited()? "Unlimited" : bucket.getSlots().slots()) + "</b></span>"));
add(new Html("<span>Locked: <b>" + (bucket.isLocked()? "Yes" : "No") + "</b></span>"));
add(bucketInfo = new VerticalLayout());
bucketInfo.setPadding(false);
updateBucketInfo();
add(lockBucketButton = new Button("", event -> changeBucketLockedState()));
setLockBucketButtonState();
var payload = new TextArea("Payload(" + bucket.getPayload().length() + ")");
payload.setValue(bucket.getPayload());
@ -99,4 +115,32 @@ public class BucketsPage extends VerticalLayout implements BeforeEnterObserver {
payload.setMinWidth(50, Unit.REM);
add(payload);
}
private void setLockBucketButtonState() {
if (bucket.isLocked()) {
lockBucketButton.setText("Unlock");
lockBucketButton.setPrefixComponent(new Icon(VaadinIcon.UNLOCK));
} else {
lockBucketButton.setText("Lock");
lockBucketButton.setPrefixComponent(new Icon(VaadinIcon.LOCK));
}
}
private void changeBucketLockedState() {
var newValue = !bucket.isLocked();
try {
bucketRepository.lock(bucket, newValue);
} catch (Error error) {
Notification.show(error.getMessage(), 3000, Notification.Position.TOP_END)
.addThemeVariants(NotificationVariant.LUMO_ERROR);
return;
}
bucket.setLocked(newValue);
setLockBucketButtonState();
updateBucketInfo();
Notification.show("Success", 3000, Notification.Position.TOP_END)
.addThemeVariants(NotificationVariant.LUMO_SUCCESS);
}
}

View File

@ -15,4 +15,6 @@ public interface BucketRepository {
void remove(Bucket bucket);
Optional<Bucket> find(Node node, String identifier);
void lock(Bucket bucket, boolean value);
}

View File

@ -79,4 +79,16 @@ public class BucketRepositoryImpl implements BucketRepository {
return Optional.empty();
}
}
@Override
public void lock(Bucket bucket, boolean value) {
try {
rest.post(URI.create(bucket.createApiURI() + "/lock"), Boolean.class, params -> {
params.put("state", Boolean.toString(value));
});
} catch (Exception ex) {
log.throwing(ex);
throw new Error("Error when changing bucket locked state");
}
}
}

View File

@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.*;
import ru.dragonestia.loadbalancer.controller.response.BucketInfoResponse;
import ru.dragonestia.loadbalancer.controller.response.BucketListResponse;
import ru.dragonestia.loadbalancer.controller.response.BucketRegisterResponse;
import ru.dragonestia.loadbalancer.controller.response.NodeRegisterResponse;
import ru.dragonestia.loadbalancer.model.Bucket;
import ru.dragonestia.loadbalancer.model.type.SlotLimit;
import ru.dragonestia.loadbalancer.service.BucketService;
@ -89,4 +88,28 @@ public class BucketController {
return bucketOpt.map(bucket -> ResponseEntity.ok(new BucketInfoResponse(bucket)))
.orElseGet(() -> ResponseEntity.notFound().build());
}
@PostMapping("/{identifier}/lock")
ResponseEntity<Boolean> lockBucket(@PathVariable("nodeIdentifier") String nodeId,
@PathVariable("identifier") String bucketId,
@RequestParam(name = "state") boolean value) {
if (!NamingValidator.validateNodeIdentifier(nodeId) || !NamingValidator.validateBucketIdentifier(bucketId)) {
return ResponseEntity.notFound().build();
}
var nodeOpt = nodeService.findNode(nodeId);
if (nodeOpt.isEmpty()) {
return ResponseEntity.notFound().build();
}
var bucketOpt = bucketService.findBucket(Objects.requireNonNull(nodeOpt.get()), bucketId);
if (bucketOpt.isEmpty()) {
return ResponseEntity.notFound().build();
}
var bucket = bucketOpt.get();
bucket.setLocked(value);
return ResponseEntity.ok(true);
}
}