1 import { Component, EventEmitter, OnInit, ViewChild } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
7 import { BsModalService } from 'ngx-bootstrap/modal';
8 import { TabsetComponent } from 'ngx-bootstrap/tabs';
9 import { TooltipDirective } from 'ngx-bootstrap/tooltip';
10 import { Subscription } from 'rxjs';
12 import { CrushRuleService } from '../../../shared/api/crush-rule.service';
13 import { ErasureCodeProfileService } from '../../../shared/api/erasure-code-profile.service';
14 import { PoolService } from '../../../shared/api/pool.service';
15 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
16 import { SelectOption } from '../../../shared/components/select/select-option.model';
17 import { ActionLabelsI18n, URLVerbs } from '../../../shared/constants/app.constants';
18 import { Icons } from '../../../shared/enum/icons.enum';
19 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
20 import { CdValidators } from '../../../shared/forms/cd-validators';
22 RbdConfigurationEntry,
23 RbdConfigurationSourceField
24 } from '../../../shared/models/configuration';
25 import { CrushRule, CrushRuleConfig } from '../../../shared/models/crush-rule';
26 import { CrushStep } from '../../../shared/models/crush-step';
27 import { ErasureCodeProfile } from '../../../shared/models/erasure-code-profile';
28 import { FinishedTask } from '../../../shared/models/finished-task';
29 import { Permission } from '../../../shared/models/permissions';
30 import { PoolFormInfo } from '../../../shared/models/pool-form-info';
31 import { DimlessBinaryPipe } from '../../../shared/pipes/dimless-binary.pipe';
32 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
33 import { FormatterService } from '../../../shared/services/formatter.service';
34 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
35 import { CrushRuleFormModalComponent } from '../crush-rule-form-modal/crush-rule-form-modal.component';
36 import { ErasureCodeProfileFormModalComponent } from '../erasure-code-profile-form/erasure-code-profile-form-modal.component';
37 import { Pool } from '../pool';
38 import { PoolFormData } from './pool-form-data';
40 interface FormFieldDescription {
41 externalFieldName: string;
42 formControlName: string;
50 selector: 'cd-pool-form',
51 templateUrl: './pool-form.component.html',
52 styleUrls: ['./pool-form.component.scss']
54 export class PoolFormComponent implements OnInit {
55 @ViewChild('crushInfoTabs', { static: false }) crushInfoTabs: TabsetComponent;
56 @ViewChild('crushDeletionBtn', { static: false }) crushDeletionBtn: TooltipDirective;
58 permission: Permission;
60 ecProfiles: ErasureCodeProfile[];
62 routeParamsSubscribe: any;
66 data = new PoolFormData(this.i18n);
67 externalPgChange = false;
68 current: Record<string, any> = {
71 initializeConfigData = new EventEmitter<{
72 initialData: RbdConfigurationEntry[];
73 sourceType: RbdConfigurationSourceField;
75 currentConfigurationValues: { [configKey: string]: any } = {};
79 pgAutoscaleModes: string[];
80 crushUsage: string[] = undefined; // Will only be set if a rule is used by some pool
82 private modalSubscription: Subscription;
85 private dimlessBinaryPipe: DimlessBinaryPipe,
86 private route: ActivatedRoute,
87 private router: Router,
88 private modalService: BsModalService,
89 private poolService: PoolService,
90 private authStorageService: AuthStorageService,
91 private formatter: FormatterService,
92 private bsModalService: BsModalService,
93 private taskWrapper: TaskWrapperService,
94 private ecpService: ErasureCodeProfileService,
95 private crushRuleService: CrushRuleService,
97 public actionLabels: ActionLabelsI18n
99 this.editing = this.router.url.startsWith(`/pool/${URLVerbs.EDIT}`);
100 this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.CREATE;
101 this.resource = this.i18n('pool');
107 this.permission = this.authStorageService.getPermissions().pool;
109 !this.permission.read ||
110 (!this.permission.update && this.editing) ||
111 (!this.permission.create && !this.editing)
113 this.router.navigate(['/404']);
117 private createForm() {
118 const compressionForm = new CdFormGroup({
119 mode: new FormControl('none'),
120 algorithm: new FormControl(''),
121 minBlobSize: new FormControl('', {
124 maxBlobSize: new FormControl('', {
127 ratio: new FormControl('', {
132 this.form = new CdFormGroup(
134 name: new FormControl('', {
136 Validators.pattern(/^[.A-Za-z0-9_/-]+$/),
138 CdValidators.custom('rbdPool', () => {
141 this.form.getValue('name').includes('/') &&
143 this.data.applications.selected.indexOf('rbd') !== -1
148 poolType: new FormControl('', {
149 validators: [Validators.required]
151 crushRule: new FormControl(null, {
155 (rule: any) => this.info && rule && this.info.osd_count < rule.min_size
160 this.isReplicated && this.info.crush_rules_replicated.length > 0 && !rule
164 size: new FormControl('', {
167 erasureProfile: new FormControl(null),
168 pgNum: new FormControl('', {
169 validators: [Validators.required, Validators.min(1)]
171 pgAutoscaleMode: new FormControl(null),
172 ecOverwrites: new FormControl(false),
173 compression: compressionForm,
174 max_bytes: new FormControl(''),
175 max_objects: new FormControl(0, {
176 validators: [Validators.min(0)]
179 [CdValidators.custom('form', (): null => null)]
184 this.poolService.getInfo().subscribe((info: PoolFormInfo) => {
189 this.setAvailableApps();
191 this.listenToChanges();
192 this.setComplexValidators();
196 private initInfo(info: PoolFormInfo) {
197 this.pgAutoscaleModes = info.pg_autoscale_modes;
198 this.form.silentSet('pgAutoscaleMode', info.pg_autoscale_default_mode);
199 this.form.silentSet('algorithm', info.bluestore_compression_algorithm);
201 this.initEcp(info.erasure_code_profiles);
204 private initEcp(ecProfiles: ErasureCodeProfile[]) {
205 this.setListControlStatus('erasureProfile', ecProfiles);
206 this.ecProfiles = ecProfiles;
210 * Used to update the crush rule or erasure code profile listings.
212 * If only one rule or profile exists it will be selected.
213 * If nothing exists null will be selected.
214 * If more than one rule or profile exists the listing will be enabled,
215 * otherwise disabled.
217 private setListControlStatus(controlName: string, arr: any[]) {
218 const control = this.form.get(controlName);
219 const value = control.value;
220 if (arr.length === 1 && (!value || !_.isEqual(value, arr[0]))) {
221 control.setValue(arr[0]);
222 } else if (arr.length === 0 && value) {
223 control.setValue(null);
225 if (arr.length <= 1) {
226 if (control.enabled) {
229 } else if (control.disabled) {
234 private initEditMode() {
235 this.disableForEdit();
236 this.routeParamsSubscribe = this.route.params.subscribe((param: { name: string }) =>
237 this.poolService.get(param.name).subscribe((pool: Pool) => {
238 this.data.pool = pool;
239 this.initEditFormData(pool);
244 private disableForEdit() {
245 ['poolType', 'crushRule', 'size', 'erasureProfile', 'ecOverwrites'].forEach((controlName) =>
246 this.form.get(controlName).disable()
250 private initEditFormData(pool: Pool) {
251 this.initializeConfigData.emit({
252 initialData: pool.configuration,
253 sourceType: RbdConfigurationSourceField.pool
255 this.poolTypeChange(pool.type);
256 const rules = this.info.crush_rules_replicated.concat(this.info.crush_rules_erasure);
258 name: pool.pool_name,
260 crushRule: rules.find((rule: CrushRule) => rule.rule_name === pool.crush_rule),
262 erasureProfile: this.ecProfiles.find((ecp) => ecp.name === pool.erasure_code_profile),
263 pgAutoscaleMode: pool.pg_autoscale_mode,
265 ecOverwrites: pool.flags_names.includes('ec_overwrites'),
266 mode: pool.options.compression_mode,
267 algorithm: pool.options.compression_algorithm,
268 minBlobSize: this.dimlessBinaryPipe.transform(pool.options.compression_min_blob_size),
269 maxBlobSize: this.dimlessBinaryPipe.transform(pool.options.compression_max_blob_size),
270 ratio: pool.options.compression_required_ratio,
271 max_bytes: this.dimlessBinaryPipe.transform(pool.quota_max_bytes),
272 max_objects: pool.quota_max_objects
274 Object.keys(dataMap).forEach((controlName: string) => {
275 const value = dataMap[controlName];
276 if (!_.isUndefined(value) && value !== '') {
277 this.form.silentSet(controlName, value);
280 this.data.pgs = this.form.getValue('pgNum');
281 this.setAvailableApps(this.data.applications.default.concat(pool.application_metadata));
282 this.data.applications.selected = pool.application_metadata;
285 private setAvailableApps(apps: string[] = this.data.applications.default) {
286 this.data.applications.available = _.uniq(apps.sort()).map(
287 (x: string) => new SelectOption(false, x, '')
291 private listenToChanges() {
292 this.listenToChangesDuringAddEdit();
294 this.listenToChangesDuringAdd();
298 private listenToChangesDuringAddEdit() {
299 this.form.get('pgNum').valueChanges.subscribe((pgs) => {
300 const change = pgs - this.data.pgs;
301 if (Math.abs(change) !== 1 || pgs === 2) {
305 this.doPgPowerJump(change as 1 | -1);
309 private doPgPowerJump(jump: 1 | -1) {
310 const power = this.calculatePgPower() + jump;
311 this.setPgs(jump === -1 ? Math.round(power) : Math.floor(power));
314 private calculatePgPower(pgs = this.form.getValue('pgNum')): number {
315 return Math.log(pgs) / Math.log(2);
318 private setPgs(power: number) {
319 const pgs = Math.pow(2, power < 0 ? 0 : power); // Set size the nearest accurate size.
321 this.form.silentSet('pgNum', pgs);
324 private listenToChangesDuringAdd() {
325 this.form.get('poolType').valueChanges.subscribe((poolType) => {
326 this.poolTypeChange(poolType);
328 this.form.get('crushRule').valueChanges.subscribe((rule) => {
329 // The crush rule can only be changed if type 'replicated' is set.
330 if (this.crushDeletionBtn && this.crushDeletionBtn.isOpen) {
331 this.crushDeletionBtn.hide();
336 this.crushRuleIsUsedBy(rule.rule_name);
337 this.replicatedRuleChange();
340 this.form.get('size').valueChanges.subscribe(() => {
341 // The size can only be changed if type 'replicated' is set.
344 this.form.get('erasureProfile').valueChanges.subscribe(() => {
345 // The ec profile can only be changed if type 'erasure' is set.
348 this.form.get('mode').valueChanges.subscribe(() => {
349 ['minBlobSize', 'maxBlobSize', 'ratio'].forEach((name) => {
350 this.form.get(name).updateValueAndValidity({ emitEvent: false });
353 this.form.get('minBlobSize').valueChanges.subscribe(() => {
354 this.form.get('maxBlobSize').updateValueAndValidity({ emitEvent: false });
356 this.form.get('maxBlobSize').valueChanges.subscribe(() => {
357 this.form.get('minBlobSize').updateValueAndValidity({ emitEvent: false });
361 private poolTypeChange(poolType: string) {
362 if (poolType === 'replicated') {
363 this.setTypeBooleans(true, false);
364 } else if (poolType === 'erasure') {
365 this.setTypeBooleans(false, true);
367 this.setTypeBooleans(false, false);
369 if (!poolType || !this.info) {
370 this.current.rules = [];
373 const rules = this.info['crush_rules_' + poolType] || [];
374 this.current.rules = rules;
378 if (this.isReplicated) {
379 this.setListControlStatus('crushRule', rules);
381 this.replicatedRuleChange();
385 private setTypeBooleans(replicated: boolean, erasure: boolean) {
386 this.isReplicated = replicated;
387 this.isErasure = erasure;
390 private replicatedRuleChange() {
391 if (!this.isReplicated) {
394 const control = this.form.get('size');
395 let size = this.form.getValue('size') || 3;
396 const min = this.getMinSize();
397 const max = this.getMaxSize();
400 } else if (size > max) {
403 if (size !== control.value) {
404 this.form.silentSet('size', size);
408 getMinSize(): number {
409 if (!this.info || this.info.osd_count < 1) {
412 const rule = this.form.getValue('crushRule');
414 return rule.min_size;
419 getMaxSize(): number {
420 if (!this.info || this.info.osd_count < 1) {
423 const osds: number = this.info.osd_count;
424 if (this.form.getValue('crushRule')) {
425 const max: number = this.form.get('crushRule').value.max_size;
434 const poolType = this.form.getValue('poolType');
435 if (!this.info || this.form.get('pgNum').dirty || !poolType) {
438 const pgMax = this.info.osd_count * 100;
439 const pgs = this.isReplicated ? this.replicatedPgCalc(pgMax) : this.erasurePgCalc(pgMax);
443 const oldValue = this.data.pgs;
445 const newValue = this.data.pgs;
446 if (!this.externalPgChange) {
447 this.externalPgChange = oldValue !== newValue;
451 private replicatedPgCalc(pgs: number): number {
452 const sizeControl = this.form.get('size');
453 const size = sizeControl.value;
454 return sizeControl.valid && size > 0 ? pgs / size : 0;
457 private erasurePgCalc(pgs: number): number {
458 const ecpControl = this.form.get('erasureProfile');
459 const ecp = ecpControl.value;
460 return (ecpControl.valid || ecpControl.disabled) && ecp ? pgs / (ecp.k + ecp.m) : 0;
463 private alignPgs(pgs = this.form.getValue('pgNum')) {
464 this.setPgs(Math.round(this.calculatePgPower(pgs < 1 ? 1 : pgs)));
467 private setComplexValidators() {
472 this.form.get('name').validator,
478 this.info.pool_names.indexOf(name) !== -1 &&
479 this.info.pool_names.indexOf(name) !==
480 this.info.pool_names.indexOf(this.data.pool.pool_name)
484 CdValidators.validateIf(this.form.get('size'), () => this.isReplicated, [
487 (value: number) => this.form.getValue('size') && value < this.getMinSize()
491 (value: number) => this.form.getValue('size') && this.getMaxSize() < value
497 this.form.get('name').validator,
500 (name: string) => this.info && this.info.pool_names.indexOf(name) !== -1
504 this.setCompressionValidators();
507 private setCompressionValidators() {
508 CdValidators.validateIf(this.form.get('minBlobSize'), () => this.hasCompressionEnabled(), [
510 CdValidators.custom('maximum', (size: string) =>
511 this.oddBlobSize(size, this.form.getValue('maxBlobSize'))
514 CdValidators.validateIf(this.form.get('maxBlobSize'), () => this.hasCompressionEnabled(), [
516 CdValidators.custom('minimum', (size: string) =>
517 this.oddBlobSize(this.form.getValue('minBlobSize'), size)
520 CdValidators.validateIf(this.form.get('ratio'), () => this.hasCompressionEnabled(), [
526 private oddBlobSize(minimum: string, maximum: string) {
527 const min = this.formatter.toBytes(minimum);
528 const max = this.formatter.toBytes(maximum);
529 return Boolean(min && max && min >= max);
532 hasCompressionEnabled() {
533 return this.form.getValue('mode') && this.form.get('mode').value.toLowerCase() !== 'none';
536 describeCrushStep(step: CrushStep) {
538 step.op.replace('_', ' '),
539 step.item_name || '',
540 step.type ? step.num + ' type ' + step.type : ''
544 addErasureCodeProfile() {
545 this.modalSubscription = this.modalService.onHide.subscribe(() => this.reloadECPs());
546 this.bsModalService.show(ErasureCodeProfileFormModalComponent);
549 private reloadECPs() {
550 this.ecpService.list().subscribe((profiles: ErasureCodeProfile[]) => this.initEcp(profiles));
551 this.modalSubscription.unsubscribe();
554 deleteErasureCodeProfile() {
555 const ecp = this.form.getValue('erasureProfile');
559 const name = ecp.name;
560 this.modalSubscription = this.modalService.onHide.subscribe(() => this.reloadECPs());
561 this.modalService.show(CriticalConfirmationModalComponent, {
563 itemDescription: this.i18n('erasure code profile'),
565 submitActionObservable: () =>
566 this.taskWrapper.wrapTaskAroundCall({
567 task: new FinishedTask('ecp/delete', { name: name }),
568 call: this.ecpService.delete(name)
575 if (this.crushDeletionBtn.isOpen) {
576 this.crushDeletionBtn.hide();
578 const modalRef = this.bsModalService.show(CrushRuleFormModalComponent);
579 modalRef.content.submitAction.subscribe((rule: CrushRuleConfig) => {
580 this.reloadCrushRules(rule.name);
584 private reloadCrushRules(ruleName?: string) {
585 if (this.modalSubscription) {
586 this.modalSubscription.unsubscribe();
588 this.poolService.getInfo().subscribe((info: PoolFormInfo) => {
590 this.poolTypeChange('replicated');
594 const newRule = this.info.crush_rules_replicated.find((rule) => rule.rule_name === ruleName);
596 this.form.get('crushRule').setValue(newRule);
602 const rule = this.form.getValue('crushRule');
606 if (this.crushUsage) {
607 this.crushDeletionBtn.toggle();
608 this.data.crushInfo = true;
610 if (this.crushInfoTabs) {
611 this.crushInfoTabs.tabs[2].active = true;
616 const name = rule.rule_name;
617 this.modalSubscription = this.modalService.onHide.subscribe(() => this.reloadCrushRules());
618 this.modalService.show(CriticalConfirmationModalComponent, {
620 itemDescription: this.i18n('crush rule'),
622 submitActionObservable: () =>
623 this.taskWrapper.wrapTaskAroundCall({
624 task: new FinishedTask('crushRule/delete', { name: name }),
625 call: this.crushRuleService.delete(name)
631 crushRuleIsUsedBy(ruleName: string) {
632 this.crushUsage = ruleName ? this.info.used_rules[ruleName] : undefined;
636 if (this.form.invalid) {
637 this.form.setErrors({ cdSubmitButton: true });
641 const pool = { pool: this.form.getValue('name') };
643 this.assignFormFields(pool, [
644 { externalFieldName: 'pool_type', formControlName: 'poolType' },
646 externalFieldName: 'pg_autoscale_mode',
647 formControlName: 'pgAutoscaleMode',
651 externalFieldName: 'pg_num',
652 formControlName: 'pgNum',
653 replaceFn: (value: number) => (this.form.getValue('pgAutoscaleMode') === 'on' ? 1 : value),
657 ? { externalFieldName: 'size', formControlName: 'size' }
659 externalFieldName: 'erasure_code_profile',
660 formControlName: 'erasureProfile',
664 externalFieldName: 'rule_name',
665 formControlName: 'crushRule',
666 replaceFn: (value: CrushRule) => (this.isReplicated ? value && value.rule_name : undefined)
669 externalFieldName: 'quota_max_bytes',
670 formControlName: 'max_bytes',
671 replaceFn: this.formatter.toBytes,
673 resetValue: this.editing ? 0 : undefined
676 externalFieldName: 'quota_max_objects',
677 formControlName: 'max_objects',
679 resetValue: this.editing ? 0 : undefined
683 if (this.info.is_all_bluestore) {
684 this.assignFormField(pool, {
685 externalFieldName: 'flags',
686 formControlName: 'ecOverwrites',
687 replaceFn: () => (this.isErasure ? ['ec_overwrites'] : undefined)
690 if (this.form.getValue('mode') !== 'none') {
691 this.assignFormFields(pool, [
693 externalFieldName: 'compression_mode',
694 formControlName: 'mode',
696 replaceFn: (value: boolean) => this.hasCompressionEnabled() && value
699 externalFieldName: 'compression_algorithm',
700 formControlName: 'algorithm',
704 externalFieldName: 'compression_min_blob_size',
705 formControlName: 'minBlobSize',
706 replaceFn: this.formatter.toBytes,
711 externalFieldName: 'compression_max_blob_size',
712 formControlName: 'maxBlobSize',
713 replaceFn: this.formatter.toBytes,
718 externalFieldName: 'compression_required_ratio',
719 formControlName: 'ratio',
724 } else if (this.editing) {
725 this.assignFormFields(pool, [
727 externalFieldName: 'compression_mode',
728 formControlName: 'mode',
730 replaceFn: () => 'unset' // Is used if no compression is set
733 externalFieldName: 'srcpool',
734 formControlName: 'name',
736 replaceFn: () => this.data.pool.pool_name
742 const apps = this.data.applications.selected;
743 if (apps.length > 0 || this.editing) {
744 pool['application_metadata'] = apps;
747 // Only collect configuration data for replicated pools, as QoS cannot be configured on EC
748 // pools. EC data pools inherit their settings from the corresponding replicated metadata pool.
749 if (this.isReplicated && !_.isEmpty(this.currentConfigurationValues)) {
750 pool['configuration'] = this.currentConfigurationValues;
753 this.triggerApiTask(pool);
757 * Retrieves the values for the given form field descriptions and assigns the values to the given
758 * object. This method differentiates between `add` and `edit` mode and acts differently on one or
761 private assignFormFields(pool: object, formFieldDescription: FormFieldDescription[]): void {
762 formFieldDescription.forEach((item) => this.assignFormField(pool, item));
766 * Retrieves the value for the given form field description and assigns the values to the given
767 * object. This method differentiates between `add` and `edit` mode and acts differently on one or
770 private assignFormField(
779 }: FormFieldDescription
781 if (this.editing && (!editable || this.form.get(formControlName).pristine)) {
784 const value = this.form.getValue(formControlName);
785 let apiValue = replaceFn ? replaceFn(value) : attr ? _.get(value, attr) : value;
786 if (!value || !apiValue) {
787 if (editable && !_.isUndefined(resetValue)) {
788 apiValue = resetValue;
793 pool[externalFieldName] = apiValue;
796 private triggerApiTask(pool: Record<string, any>) {
798 .wrapTaskAroundCall({
799 task: new FinishedTask('pool/' + (this.editing ? URLVerbs.EDIT : URLVerbs.CREATE), {
800 pool_name: pool.hasOwnProperty('srcpool') ? pool.srcpool : pool.pool
802 call: this.poolService[this.editing ? URLVerbs.UPDATE : URLVerbs.CREATE](pool)
807 if (_.isObject(resp.error) && resp.error.code === '34') {
808 this.form.get('pgNum').setErrors({ '34': true });
810 this.form.setErrors({ cdSubmitButton: true });
812 () => this.router.navigate(['/pool'])
817 this.form.get('name').updateValueAndValidity({ emitEvent: false, onlySelf: true });