Search in sources :

Example 1 with ThreadPoolSettingsModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method getMediumPriorityThreadPoolSettings.

@ApiOperation(value = "Get the Medium Priority Service Thread Pool Settings", notes = "active count and largest pool size are read only")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/medium-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> getMediumPriorityThreadPoolSettings(HttpServletRequest request) {
    RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            int corePoolSize = Common.backgroundProcessing.getMediumPriorityServiceCorePoolSize();
            int maximumPoolSize = Common.backgroundProcessing.getMediumPriorityServiceMaximumPoolSize();
            int activeCount = Common.backgroundProcessing.getMediumPriorityServiceActiveCount();
            int largestPoolSize = Common.backgroundProcessing.getMediumPriorityServiceLargestPoolSize();
            ThreadPoolSettingsModel model = new ThreadPoolSettingsModel(corePoolSize, maximumPoolSize, activeCount, largestPoolSize);
            return result.createResponseEntity(model);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to access medium priority thread pool settings.");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ThreadPoolSettingsModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method setHighPrioritySettings.

@ApiOperation(value = "Update high priority queue settings", notes = "")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, value = "/high-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> setHighPrioritySettings(@ApiParam(value = "Settings", required = true, allowMultiple = false) @RequestBody ThreadPoolSettingsModel model, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            // Validate the settings
            ThreadPoolExecutor executor = (ThreadPoolExecutor) Common.timer.getExecutorService();
            int currentCorePoolSize = executor.getCorePoolSize();
            int currentMaxPoolSize = executor.getMaximumPoolSize();
            if ((model.getMaximumPoolSize() != null) && (model.getMaximumPoolSize() < BackgroundProcessing.HIGH_PRI_MAX_POOL_SIZE_MIN)) {
                // Test to ensure we aren't setting too low
                model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.greaterThanOrEqualTo", BackgroundProcessing.HIGH_PRI_MAX_POOL_SIZE_MIN), RestMessageLevel.ERROR, "corePoolSize"));
                result.addRestMessage(this.getValidationFailedError());
            } else if (!validate(model, currentCorePoolSize, currentMaxPoolSize)) {
                result.addRestMessage(this.getValidationFailedError());
            } else {
                if (model.getCorePoolSize() != null) {
                    executor.setCorePoolSize(model.getCorePoolSize());
                    SystemSettingsDao.instance.setIntValue(SystemSettingsDao.HIGH_PRI_CORE_POOL_SIZE, model.getCorePoolSize());
                } else {
                    // Get the info for the user
                    int corePoolSize = executor.getCorePoolSize();
                    model.setCorePoolSize(corePoolSize);
                }
                if (model.getMaximumPoolSize() != null) {
                    executor.setMaximumPoolSize(model.getMaximumPoolSize());
                    SystemSettingsDao.instance.setIntValue(SystemSettingsDao.HIGH_PRI_MAX_POOL_SIZE, model.getMaximumPoolSize());
                } else {
                    // Get the info for the user
                    int maximumPoolSize = executor.getMaximumPoolSize();
                    model.setMaximumPoolSize(maximumPoolSize);
                }
                // Get the settings for the model
                int activeCount = executor.getActiveCount();
                int largestPoolSize = executor.getLargestPoolSize();
                model.setActiveCount(activeCount);
                model.setLargestPoolSize(largestPoolSize);
            }
            return result.createResponseEntity(model);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to set high priority thread pool settings.");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ThreadPoolSettingsModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method setMediumPrioritySettings.

@ApiOperation(value = "Update medium priority queue settings", notes = "Only corePoolSize and maximumPoolSize are used")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, value = "/medium-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> setMediumPrioritySettings(@ApiParam(value = "Settings", required = true, allowMultiple = false) @RequestBody ThreadPoolSettingsModel model, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            // Validate the settings
            int currentCorePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.MED_PRI_CORE_POOL_SIZE);
            if ((model.getCorePoolSize() != null) && (model.getCorePoolSize() < BackgroundProcessing.MED_PRI_MAX_POOL_SIZE_MIN)) {
                // Test to ensure we aren't setting too low
                model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.greaterThanOrEqualTo", BackgroundProcessing.MED_PRI_MAX_POOL_SIZE_MIN), RestMessageLevel.ERROR, "corePoolSize"));
                result.addRestMessage(this.getValidationFailedError());
            } else if (!validate(model, currentCorePoolSize, model.getCorePoolSize() == null ? currentCorePoolSize : model.getCorePoolSize())) {
                result.addRestMessage(this.getValidationFailedError());
            } else {
                if (model.getCorePoolSize() != null) {
                    Common.backgroundProcessing.setMediumPriorityServiceCorePoolSize(model.getCorePoolSize());
                    SystemSettingsDao.instance.setIntValue(SystemSettingsDao.MED_PRI_CORE_POOL_SIZE, model.getCorePoolSize());
                } else {
                    // Get the info for the user
                    int corePoolSize = Common.backgroundProcessing.getMediumPriorityServiceCorePoolSize();
                    model.setCorePoolSize(corePoolSize);
                }
                if (model.getMaximumPoolSize() == null) {
                    // Get the info for the user
                    int maximumPoolSize = Common.backgroundProcessing.getMediumPriorityServiceMaximumPoolSize();
                    model.setMaximumPoolSize(maximumPoolSize);
                }
                // Get the settings for the model
                int activeCount = Common.backgroundProcessing.getMediumPriorityServiceActiveCount();
                int largestPoolSize = Common.backgroundProcessing.getMediumPriorityServiceLargestPoolSize();
                model.setActiveCount(activeCount);
                model.setLargestPoolSize(largestPoolSize);
            }
            return result.createResponseEntity(model);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to set medium priority thread pool settings.");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ThreadPoolSettingsModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method validate.

/**
 * Helper for validation
 * @param model
 * @param currentCorePoolSize
 * @param currentMaximumPoolSize
 * @return
 */
protected boolean validate(ThreadPoolSettingsModel model, int currentCorePoolSize, int currentMaximumPoolSize) {
    boolean passed = true;
    model.setMessages(new ArrayList<RestValidationMessage>());
    if ((model.getCorePoolSize() != null) && (model.getCorePoolSize() < 1)) {
        model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.greaterThanZero"), RestMessageLevel.ERROR, "corePoolSize"));
        passed = false;
    }
    // Compare both if they are being added
    if ((model.getMaximumPoolSize() != null) && (model.getCorePoolSize() != null) && (model.getMaximumPoolSize() < model.getCorePoolSize())) {
        model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.maxGreaterThanMin"), RestMessageLevel.ERROR, "maximumPoolSize"));
        passed = false;
    }
    // Compare the max pool size to the existing if we aren't changing the Core size
    if ((model.getCorePoolSize() == null) && (model.getMaximumPoolSize() != null)) {
        if (model.getMaximumPoolSize() < currentCorePoolSize) {
            model.setCorePoolSize(currentCorePoolSize);
            model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.maxGreaterThanMin"), RestMessageLevel.ERROR, "maximumPoolSize"));
            passed = false;
        }
    }
    // Compare the max pool size to the existing if we aren't changing the Core size
    if ((model.getMaximumPoolSize() == null) && (model.getCorePoolSize() != null)) {
        if (model.getCorePoolSize() > currentMaximumPoolSize) {
            model.setMaximumPoolSize(currentMaximumPoolSize);
            model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.minLessThanMax"), RestMessageLevel.ERROR, "corePoolSize"));
            passed = false;
        }
    }
    return passed;
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 5 with ThreadPoolSettingsModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method getLowPriorityThreadPoolSettings.

@ApiOperation(value = "Get the Low Priority Service Thread Pool Settings", notes = "active count and largest pool size are read only")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/low-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> getLowPriorityThreadPoolSettings(HttpServletRequest request) {
    RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            int corePoolSize = Common.backgroundProcessing.getLowPriorityServiceCorePoolSize();
            int maximumPoolSize = Common.backgroundProcessing.getLowPriorityServiceMaximumPoolSize();
            int activeCount = Common.backgroundProcessing.getLowPriorityServiceActiveCount();
            int largestPoolSize = Common.backgroundProcessing.getLowPriorityServiceLargestPoolSize();
            ThreadPoolSettingsModel model = new ThreadPoolSettingsModel(corePoolSize, maximumPoolSize, activeCount, largestPoolSize);
            return result.createResponseEntity(model);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to access low priority thread pool settings.");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

User (com.serotonin.m2m2.vo.User)6 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)6 ThreadPoolSettingsModel (com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel)6 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 RestValidationMessage (com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage)4 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2