Home / Function/ run() — netty Function Reference

run() — netty Function Reference

Architecture documentation for the run() function in AutoScalingEventExecutorChooserFactory.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  0051a9bb_2eac_9138_da01_67fb084baff0["run()"]
  d9aa57c9_e2b2_7790_035b_2a28f5df0c47["UtilizationMonitor"]
  0051a9bb_2eac_9138_da01_67fb084baff0 -->|defined in| d9aa57c9_e2b2_7790_035b_2a28f5df0c47
  197d4f82_a16f_13ca_c844_43ca49319a2d["setUtilization()"]
  0051a9bb_2eac_9138_da01_67fb084baff0 -->|calls| 197d4f82_a16f_13ca_c844_43ca49319a2d
  a4304eb6_db6c_91df_93c2_2b3acb53fc85["tryScaleUpBy()"]
  0051a9bb_2eac_9138_da01_67fb084baff0 -->|calls| a4304eb6_db6c_91df_93c2_2b3acb53fc85
  c54d4b63_d04e_6ce4_af65_d57b7c4c37f3["rebuildActiveExecutors()"]
  0051a9bb_2eac_9138_da01_67fb084baff0 -->|calls| c54d4b63_d04e_6ce4_af65_d57b7c4c37f3
  style 0051a9bb_2eac_9138_da01_67fb084baff0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

common/src/main/java/io/netty/util/concurrent/AutoScalingEventExecutorChooserFactory.java lines 272–392

            @Override
            public void run() {
                if (executors.length == 0 || executors[0].isShuttingDown()) {
                    // The group is shutting down, so no scaling decisions should be made.
                    // The lifecycle listener on the terminationFuture will handle the final cancellation.
                    return;
                }

                // Calculate the actual elapsed time since the last run.
                final long now = executors[0].ticker().nanoTime();
                long totalTime;

                if (lastCheckTimeNanos == 0) {
                    // On the first run, use the configured period as a baseline to avoid skipping the cycle.
                    totalTime = utilizationCheckPeriodNanos;
                } else {
                    // On subsequent runs, calculate the actual elapsed time.
                    totalTime = now - lastCheckTimeNanos;
                }

                // Always update the timestamp for the next cycle.
                lastCheckTimeNanos = now;

                if (totalTime <= 0) {
                    // Skip this cycle if the clock has issues or the interval is invalid.
                    return;
                }

                int consistentlyBusyChildren = 0;
                consistentlyIdleChildren.clear();

                final AutoScalingState currentState = state.get();

                for (int i = 0; i < executors.length; i++) {
                    EventExecutor child = executors[i];
                    if (!(child instanceof SingleThreadEventExecutor)) {
                        continue;
                    }

                    SingleThreadEventExecutor eventExecutor = (SingleThreadEventExecutor) child;

                    double utilization = 0.0;
                    if (!eventExecutor.isSuspended()) {
                        long activeTime = eventExecutor.getAndResetAccumulatedActiveTimeNanos();

                        if (activeTime == 0) {
                            long lastActivity = eventExecutor.getLastActivityTimeNanos();
                            long idleTime = now - lastActivity;

                            // If the event loop has been idle for less time than our utilization window,
                            // it means it was active for the remainder of that window.
                            if (idleTime < totalTime) {
                                activeTime = totalTime - idleTime;
                            }
                            // If idleTime >= totalTime, it was idle for the whole window, so activeTime remains 0.
                        }

                        utilization = Math.min(1.0, (double) activeTime / totalTime);

                        if (utilization < scaleDownThreshold) {
                            // Utilization is low, increment idle counter and reset busy counter.
                            int idleCycles = eventExecutor.getAndIncrementIdleCycles();
                            eventExecutor.resetBusyCycles();
                            if (idleCycles >= scalingPatienceCycles &&
                                eventExecutor.getNumOfRegisteredChannels() <= 0) {
                                consistentlyIdleChildren.add(eventExecutor);
                            }
                        } else if (utilization > scaleUpThreshold) {
                            // Utilization is high, increment busy counter and reset idle counter.
                            int busyCycles = eventExecutor.getAndIncrementBusyCycles();
                            eventExecutor.resetIdleCycles();
                            if (busyCycles >= scalingPatienceCycles) {
                                consistentlyBusyChildren++;
                            }
                        } else {
                            // Utilization is in the normal range, reset counters.
                            eventExecutor.resetIdleCycles();
                            eventExecutor.resetBusyCycles();
                        }
                    }

Domain

Subdomains

Frequently Asked Questions

What does run() do?
run() is a function in the netty codebase, defined in common/src/main/java/io/netty/util/concurrent/AutoScalingEventExecutorChooserFactory.java.
Where is run() defined?
run() is defined in common/src/main/java/io/netty/util/concurrent/AutoScalingEventExecutorChooserFactory.java at line 272.
What does run() call?
run() calls 3 function(s): rebuildActiveExecutors, setUtilization, tryScaleUpBy.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free