Home / Class/ Entries Class — netty Architecture

Entries Class — netty Architecture

Architecture documentation for the Entries class in Cache.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  0a5c7e5a_0868_706c_64ef_570d8eda93ed["Entries"]
  0a3b4924_5315_7bb5_31fc_ae6f82962074["Cache.java"]
  0a5c7e5a_0868_706c_64ef_570d8eda93ed -->|defined in| 0a3b4924_5315_7bb5_31fc_ae6f82962074
  388be597_f6bf_76c5_5fa0_16e8d63ce694["Entries()"]
  0a5c7e5a_0868_706c_64ef_570d8eda93ed -->|method| 388be597_f6bf_76c5_5fa0_16e8d63ce694
  a3a06bb5_0cc7_b138_7d64_44cab8d3dafc["add()"]
  0a5c7e5a_0868_706c_64ef_570d8eda93ed -->|method| a3a06bb5_0cc7_b138_7d64_44cab8d3dafc
  c13d2041_e562_e9af_db02_30f08b74c5b1["scheduleCacheExpirationIfNeeded()"]
  0a5c7e5a_0868_706c_64ef_570d8eda93ed -->|method| c13d2041_e562_e9af_db02_30f08b74c5b1
  49907d70_83f6_65fd_265c_73a9a8cba80a["clearAndCancel()"]
  0a5c7e5a_0868_706c_64ef_570d8eda93ed -->|method| 49907d70_83f6_65fd_265c_73a9a8cba80a
  bd693175_180f_1e1a_9d33_3ba960b9541f["run()"]
  0a5c7e5a_0868_706c_64ef_570d8eda93ed -->|method| bd693175_180f_1e1a_9d33_3ba960b9541f

Relationship Graph

Source Code

resolver-dns/src/main/java/io/netty/resolver/dns/Cache.java lines 162–291

    private final class Entries extends AtomicReference<List<E>> implements Runnable {

        private final String hostname;
        // Needs to be package-private to be able to access it via the AtomicReferenceFieldUpdater
        volatile ScheduledFuture<?> expirationFuture;

        Entries(String hostname) {
            super(Collections.<E>emptyList());
            this.hostname = hostname;
        }

        void add(E e, int ttl, EventLoop loop) {
            if (!shouldReplaceAll(e)) {
                for (;;) {
                    List<E> entries = get();
                    if (!entries.isEmpty()) {
                        final E firstEntry = entries.get(0);
                        if (shouldReplaceAll(firstEntry)) {
                            assert entries.size() == 1;

                            if (compareAndSet(entries, singletonList(e))) {
                                scheduleCacheExpirationIfNeeded(ttl, loop);
                                return;
                            } else {
                                // Need to try again as CAS failed
                                continue;
                            }
                        }

                        // Create a new List for COW semantics
                        List<E> newEntries = new ArrayList<E>(entries.size() + 1);
                        int i = 0;
                        E replacedEntry = null;
                        do {
                            E entry = entries.get(i);
                            // Only add old entry if the address is not the same as the one we try to add as well.
                            // In this case we will skip it and just add the new entry as this may have
                            // more up-to-date data and cancel the old after we were able to update the cache.
                            if (!Cache.this.equals(e, entry)) {
                                newEntries.add(entry);
                            } else {
                                replacedEntry = entry;
                                newEntries.add(e);

                                ++i;
                                for (; i < entries.size(); ++i) {
                                    newEntries.add(entries.get(i));
                                }
                                break;
                            }
                        } while (++i < entries.size());
                        if (replacedEntry == null) {
                            newEntries.add(e);
                        }
                        sortEntries(hostname, newEntries);

                        if (compareAndSet(entries, Collections.unmodifiableList(newEntries))) {
                            scheduleCacheExpirationIfNeeded(ttl, loop);
                            return;
                        }
                    } else if (compareAndSet(entries, singletonList(e))) {
                        scheduleCacheExpirationIfNeeded(ttl, loop);
                        return;
                    }
                }
            } else {
                set(singletonList(e));
                scheduleCacheExpirationIfNeeded(ttl, loop);
            }
        }

        private void scheduleCacheExpirationIfNeeded(int ttl, EventLoop loop) {
            for (;;) {
                // We currently don't calculate a new TTL when we need to retry the CAS as we don't expect this to
                // be invoked very concurrently and also we use SECONDS anyway. If this ever becomes a problem
                // we can reconsider.
                ScheduledFuture<?> oldFuture = FUTURE_UPDATER.get(this);
                if (oldFuture == null || oldFuture.getDelay(TimeUnit.SECONDS) > ttl) {
                    ScheduledFuture<?> newFuture = loop.schedule(this, ttl, TimeUnit.SECONDS);
                    // It is possible that
                    // 1. task will fire in between this line, or

Frequently Asked Questions

What is the Entries class?
Entries is a class in the netty codebase, defined in resolver-dns/src/main/java/io/netty/resolver/dns/Cache.java.
Where is Entries defined?
Entries is defined in resolver-dns/src/main/java/io/netty/resolver/dns/Cache.java at line 162.

Analyze Your Own Codebase

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

Try Supermodel Free