35
35
import org .apache .flink .kubernetes .operator .config .FlinkConfigManager ;
36
36
import org .apache .flink .kubernetes .operator .exception .ReconciliationException ;
37
37
import org .apache .flink .kubernetes .operator .metrics .MetricManager ;
38
+ import org .apache .flink .kubernetes .operator .metrics .TestingMetricListener ;
38
39
import org .apache .flink .kubernetes .operator .observer .snapshot .StateSnapshotObserver ;
39
40
import org .apache .flink .kubernetes .operator .reconciler .ReconciliationUtils ;
40
41
import org .apache .flink .kubernetes .operator .reconciler .snapshot .StateSnapshotReconciler ;
56
57
import javax .annotation .Nullable ;
57
58
58
59
import java .time .Instant ;
60
+ import java .util .Map ;
59
61
import java .util .Optional ;
60
62
import java .util .function .BiConsumer ;
61
63
64
66
import static org .apache .flink .kubernetes .operator .api .status .FlinkStateSnapshotStatus .State .FAILED ;
65
67
import static org .apache .flink .kubernetes .operator .api .status .FlinkStateSnapshotStatus .State .IN_PROGRESS ;
66
68
import static org .apache .flink .kubernetes .operator .api .status .FlinkStateSnapshotStatus .State .TRIGGER_PENDING ;
69
+ import static org .apache .flink .kubernetes .operator .metrics .FlinkStateSnapshotMetricsUtils .assertSnapshotMetrics ;
67
70
import static org .assertj .core .api .Assertions .assertThat ;
68
71
import static org .junit .jupiter .api .Assertions .assertThrows ;
69
72
70
73
/** Test class for {@link FlinkStateSnapshotController}. */
71
74
@ EnableKubernetesMockClient (crud = true )
72
75
public class FlinkStateSnapshotControllerTest {
73
- private static final String SNAPSHOT_NAME = "snapshot-test" ;
76
+ private static final String SAVEPOINT_NAME = "savepoint-test" ;
77
+ private static final String CHECKPOINT_NAME = "checkpoint-test" ;
74
78
private static final String SAVEPOINT_PATH = "/tmp/asd" ;
75
79
private static final String JOB_ID = "fd72014d4c864993a2e5a9287b4a9c5d" ;
76
80
@@ -81,6 +85,8 @@ public class FlinkStateSnapshotControllerTest {
81
85
private FlinkStateSnapshotEventCollector flinkStateSnapshotEventCollector ;
82
86
private EventRecorder eventRecorder ;
83
87
private TestingFlinkResourceContextFactory ctxFactory ;
88
+ private TestingMetricListener listener ;
89
+ private MetricManager <FlinkStateSnapshot > metricManager ;
84
90
private StatusRecorder <FlinkStateSnapshot , FlinkStateSnapshotStatus > statusRecorder ;
85
91
private FlinkStateSnapshotController controller ;
86
92
private Context <FlinkStateSnapshot > context ;
@@ -97,14 +103,20 @@ public void beforeEach() {
97
103
TestUtils .createTestMetricGroup (new Configuration ()),
98
104
flinkService ,
99
105
eventRecorder );
100
- statusRecorder = new StatusRecorder <>(new MetricManager <>(), statusUpdateCounter );
106
+
107
+ listener = new TestingMetricListener (new Configuration ());
108
+ metricManager =
109
+ MetricManager .createFlinkStateSnapshotMetricManager (
110
+ new Configuration (), listener .getMetricGroup ());
111
+ statusRecorder = new StatusRecorder <>(metricManager , statusUpdateCounter );
101
112
controller =
102
113
new FlinkStateSnapshotController (
103
114
ValidatorUtils .discoverValidators (configManager ),
104
115
ctxFactory ,
105
116
new StateSnapshotReconciler (ctxFactory , eventRecorder ),
106
117
new StateSnapshotObserver (ctxFactory , eventRecorder ),
107
118
eventRecorder ,
119
+ metricManager ,
108
120
statusRecorder );
109
121
}
110
122
@@ -514,8 +526,10 @@ public void testReconcileJobNotFound() {
514
526
var snapshot = createSavepoint (deployment );
515
527
var errorMessage =
516
528
String .format (
517
- "Secondary resource %s (%s) for savepoint snapshot-test was not found" ,
518
- deployment .getMetadata ().getName (), CrdConstants .KIND_FLINK_DEPLOYMENT );
529
+ "Secondary resource %s (%s) for savepoint %s was not found" ,
530
+ deployment .getMetadata ().getName (),
531
+ CrdConstants .KIND_FLINK_DEPLOYMENT ,
532
+ SAVEPOINT_NAME );
519
533
520
534
// First reconcile will trigger the snapshot.
521
535
controller .reconcile (snapshot , TestUtils .createSnapshotContext (client , deployment ));
@@ -556,8 +570,10 @@ public void testReconcileJobNotRunning() {
556
570
var snapshot = createSavepoint (deployment );
557
571
var errorMessage =
558
572
String .format (
559
- "Secondary resource %s (%s) for savepoint snapshot-test is not running" ,
560
- deployment .getMetadata ().getName (), CrdConstants .KIND_FLINK_DEPLOYMENT );
573
+ "Secondary resource %s (%s) for savepoint %s is not running" ,
574
+ deployment .getMetadata ().getName (),
575
+ CrdConstants .KIND_FLINK_DEPLOYMENT ,
576
+ SAVEPOINT_NAME );
561
577
562
578
controller .reconcile (snapshot , context );
563
579
@@ -579,6 +595,37 @@ public void testReconcileJobNotRunning() {
579
595
});
580
596
}
581
597
598
+ @ Test
599
+ public void testMetrics () {
600
+ var deployment = createDeployment ();
601
+ var savepoint = createSavepoint (deployment );
602
+ savepoint .getSpec ().getSavepoint ().setDisposeOnDelete (false );
603
+ var checkpoint = createCheckpoint (deployment , CheckpointType .FULL , 1 );
604
+
605
+ context = TestUtils .createSnapshotContext (client , deployment );
606
+
607
+ controller .reconcile (savepoint , context );
608
+ controller .reconcile (savepoint , context );
609
+ controller .reconcile (savepoint , context );
610
+ assertThat (savepoint .getStatus ().getState ()).isEqualTo (COMPLETED );
611
+
612
+ controller .reconcile (checkpoint , context );
613
+ controller .reconcile (checkpoint , context );
614
+ controller .reconcile (checkpoint , context );
615
+ assertThat (checkpoint .getStatus ().getState ()).isEqualTo (COMPLETED );
616
+
617
+ assertSnapshotMetrics (
618
+ listener , TestUtils .TEST_NAMESPACE , Map .of (COMPLETED , 1 ), Map .of (COMPLETED , 1 ));
619
+
620
+ // Remove savepoint
621
+ assertDeleteControl (controller .cleanup (savepoint , context ), true , null );
622
+ assertSnapshotMetrics (listener , TestUtils .TEST_NAMESPACE , Map .of (), Map .of (COMPLETED , 1 ));
623
+
624
+ // Remove checkpoint
625
+ assertDeleteControl (controller .cleanup (checkpoint , context ), true , null );
626
+ assertSnapshotMetrics (listener , TestUtils .TEST_NAMESPACE , Map .of (), Map .of ());
627
+ }
628
+
582
629
private FlinkStateSnapshot createSavepoint (FlinkDeployment deployment ) {
583
630
return createSavepoint (deployment , false , 7 );
584
631
}
@@ -591,8 +638,8 @@ private FlinkStateSnapshot createSavepoint(
591
638
FlinkDeployment deployment , boolean alreadyExists , int backoffLimit ) {
592
639
var snapshot =
593
640
TestUtils .buildFlinkStateSnapshotSavepoint (
594
- SNAPSHOT_NAME ,
595
- "test" ,
641
+ SAVEPOINT_NAME ,
642
+ TestUtils . TEST_NAMESPACE ,
596
643
SAVEPOINT_PATH ,
597
644
alreadyExists ,
598
645
deployment == null ? null : JobReference .fromFlinkResource (deployment ));
@@ -606,8 +653,8 @@ private FlinkStateSnapshot createCheckpoint(
606
653
FlinkDeployment deployment , CheckpointType checkpointType , int backoffLimit ) {
607
654
var snapshot =
608
655
TestUtils .buildFlinkStateSnapshotCheckpoint (
609
- SNAPSHOT_NAME ,
610
- "test" ,
656
+ CHECKPOINT_NAME ,
657
+ TestUtils . TEST_NAMESPACE ,
611
658
checkpointType ,
612
659
JobReference .fromFlinkResource (deployment ));
613
660
snapshot .getSpec ().setBackoffLimit (backoffLimit );
0 commit comments