File: table_session_connect.cc

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (351 lines) | stat: -rw-r--r-- 11,206 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* Copyright (c) 2008, 2025, Oracle and/or its affiliates.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2.0,
  as published by the Free Software Foundation.

  This program is designed to work with certain software (including
  but not limited to OpenSSL) that is licensed under separate terms,
  as designated in a particular file or component or in included license
  documentation.  The authors of MySQL hereby grant you an additional
  permission to link the program and your derivative works with the
  separately licensed software that they have either included with
  the program or referenced in the documentation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License, version 2.0, for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

/**
  @file storage/perfschema/table_session_connect.cc
  TABLE SESSION_CONNECT (abstract).
*/

#include "storage/perfschema/table_session_connect.h"

#include <assert.h>
#include "my_compiler.h"

#include "my_inttypes.h"
#include "sql/field.h"
#include "sql/table.h"
#include "storage/perfschema/pfs_buffer_container.h"

bool PFS_index_session_connect::match(PFS_thread *pfs) {
  if (m_fields >= 1) {
    if (!m_key_1.match(pfs)) {
      return false;
    }
  }

  return true;
}

bool PFS_index_session_connect::match(row_session_connect_attrs *row) {
  if (m_fields >= 2) {
    if (!m_key_2.match(row->m_attr_name, row->m_attr_name_length)) {
      return false;
    }
  }

  return true;
}

table_session_connect::table_session_connect(
    const PFS_engine_table_share *share)
    : cursor_by_thread_connect_attr(share) {
  if (session_connect_attrs_size_per_thread > 0) {
    m_copy_session_connect_attrs = (char *)my_malloc(
        PSI_INSTRUMENT_ME, session_connect_attrs_size_per_thread, MYF(0));
  } else {
    m_copy_session_connect_attrs = nullptr;
  }
  m_copy_session_connect_attrs_length = 0;
}

table_session_connect::~table_session_connect() {
  my_free(m_copy_session_connect_attrs);
}

int table_session_connect::index_init(uint idx [[maybe_unused]], bool) {
  assert(idx == 0);
  m_opened_index = PFS_NEW(PFS_index_session_connect);
  m_index = m_opened_index;
  return 0;
}

int table_session_connect::index_next() {
  PFS_thread *thread;
  bool has_more_thread = true;
  int rc = 0;

  for (m_pos.set_at(&m_next_pos); has_more_thread; m_pos.next_thread()) {
    thread = global_thread_container.get(m_pos.m_index_1, &has_more_thread);
    if (thread != nullptr) {
      if (m_opened_index->match(thread)) {
        do {
          /*
            Here we materialize the row first,
            and then evaluate if it matches the index.
            This is simpler, as parsing the session attributes encoded string
            is done only once.
          */
          rc = make_row(thread, m_pos.m_index_2);

          if (rc == 0) {
            if (m_opened_index->match(&m_row)) {
              m_next_pos.set_after(&m_pos);
              return 0;
            }
            m_pos.m_index_2++;
          }
        } while (rc == 0);
      }
    }
  }

  return HA_ERR_END_OF_FILE;
}

/**
  Take a length encoded string

  @arg ptr  inout       the input string array
  @arg dest             where to store the result
  @arg dest_size        max size of @c dest
  @arg copied_len       the actual length of the data copied
  @arg start_ptr        pointer to the start of input
  @arg input_length     the length of the incoming data
  @arg from_cs          character set in which @c ptr is encoded
  @arg nchars_max       maximum number of characters to read
  @return status
    @retval true    parsing failed
    @retval false   parsing succeeded
*/
static bool parse_length_encoded_string(const char **ptr, char *dest,
                                        uint dest_size, uint *copied_len,
                                        const char *start_ptr,
                                        uint input_length,
                                        const CHARSET_INFO *from_cs,
                                        uint nchars_max) {
  ulong copy_length, data_length;
  const char *well_formed_error_pos = nullptr,
             *cannot_convert_error_pos = nullptr, *from_end_pos = nullptr;

  data_length =
      net_field_length(const_cast<uchar **>(pointer_cast<const uchar **>(ptr)));

  /* we don't tolerate NULL as a length */
  if (data_length == NULL_LENGTH) {
    return true;
  }

  if (*ptr - start_ptr + data_length > input_length) {
    return true;
  }

  /*
    TODO: Migrate the data itself to UTF8MB4,
    this is still UTF8MB3 printed in a UTF8MB4 column.
  */
  copy_length = well_formed_copy_nchars(
      &my_charset_utf8mb3_bin, dest, dest_size, from_cs, *ptr, data_length,
      nchars_max, &well_formed_error_pos, &cannot_convert_error_pos,
      &from_end_pos);
  *copied_len = copy_length;
  (*ptr) += data_length;

  return false;
}

/**
  Take the nth attribute name/value pair

  Parse the attributes blob form the beginning, skipping the attributes
  whose number is lower than the one we seek.
  When we reach the attribute at an index we're looking for the values
  are copied to the output parameters.
  If parsing fails or no more attributes are found the function stops
  and returns an error code.

  @param connect_attrs            pointer to the connect attributes blob
  @param connect_attrs_length     length of @c connect_attrs
  @param connect_attrs_cs         character set used to encode @c connect_attrs
  @param ordinal                  index of the attribute we need
  @param [out] attr_name          buffer to receive the attribute name
  @param max_attr_name            max size of @c attr_name in bytes
  @param [out] attr_name_length   number of bytes written in @c attr_name
  @param [out] attr_value         buffer to receive the attribute name
  @param max_attr_value           max size of @c attr_value in bytes
  @param [out] attr_value_length  number of bytes written in @c attr_value
  @return status
    @retval true    requested attribute pair is found and copied
    @retval false   error. Either because of parsing or too few attributes.
*/
bool read_nth_attr(const char *connect_attrs, uint connect_attrs_length,
                   const CHARSET_INFO *connect_attrs_cs, uint ordinal,
                   char *attr_name, uint max_attr_name, uint *attr_name_length,
                   char *attr_value, uint max_attr_value,
                   uint *attr_value_length) {
  uint idx;
  const char *ptr;

  for (ptr = connect_attrs, idx = 0;
       (uint)(ptr - connect_attrs) < connect_attrs_length && idx <= ordinal;
       idx++) {
    uint copy_length;

    /* read the key */
    if (parse_length_encoded_string(
            &ptr, attr_name, max_attr_name, &copy_length, connect_attrs,
            connect_attrs_length, connect_attrs_cs, 32) ||
        !copy_length) {
      return false;
    }

    if (idx == ordinal) {
      *attr_name_length = copy_length;
    }

    /* read the value */
    if (parse_length_encoded_string(
            &ptr, attr_value, max_attr_value, &copy_length, connect_attrs,
            connect_attrs_length, connect_attrs_cs, 1024)) {
      return false;
    }

    if (idx == ordinal) {
      *attr_value_length = copy_length;
    }

    if (idx == ordinal) {
      return true;
    }
  }

  return false;
}

int table_session_connect::make_row(PFS_thread *pfs, uint ordinal) {
  pfs_optimistic_state lock;
  pfs_optimistic_state session_lock;
  PFS_thread_class *safe_class;
  const CHARSET_INFO *cs;

  /* Protect this reader against thread termination */
  pfs->m_lock.begin_optimistic_lock(&lock);
  /* Protect this reader against writing on session attributes */
  pfs->m_session_lock.begin_optimistic_lock(&session_lock);

  safe_class = sanitize_thread_class(pfs->m_class);
  if (unlikely(safe_class == nullptr)) {
    return HA_ERR_RECORD_DELETED;
  }

  /* Filtering threads must be done under the protection of the optimistic lock.
   */
  if (!thread_fits(pfs)) {
    return HA_ERR_RECORD_DELETED;
  }

  /* Make a safe copy of the session attributes */

  if (m_copy_session_connect_attrs == nullptr) {
    return HA_ERR_RECORD_DELETED;
  }

  m_copy_session_connect_attrs_length = pfs->m_session_connect_attrs_length;

  if (m_copy_session_connect_attrs_length >
      session_connect_attrs_size_per_thread) {
    return HA_ERR_RECORD_DELETED;
  }

  memcpy(m_copy_session_connect_attrs, pfs->m_session_connect_attrs,
         m_copy_session_connect_attrs_length);

  cs = get_charset(pfs->m_session_connect_attrs_cs_number, MYF(0));
  if (cs == nullptr) {
    return HA_ERR_RECORD_DELETED;
  }

  if (!pfs->m_session_lock.end_optimistic_lock(&session_lock)) {
    return HA_ERR_RECORD_DELETED;
  }

  if (!pfs->m_lock.end_optimistic_lock(&lock)) {
    return HA_ERR_RECORD_DELETED;
  }

  /*
    Now we have a safe copy of the data,
    that will not change while parsing it
  */

  /* populate the row */
  if (read_nth_attr(
          m_copy_session_connect_attrs, m_copy_session_connect_attrs_length, cs,
          ordinal, m_row.m_attr_name, (uint)sizeof(m_row.m_attr_name),
          &m_row.m_attr_name_length, m_row.m_attr_value,
          (uint)sizeof(m_row.m_attr_value), &m_row.m_attr_value_length)) {
    /* we don't expect internal threads to have connection attributes */
    if (pfs->m_processlist_id == 0) {
      return HA_ERR_RECORD_DELETED;
    }

    m_row.m_ordinal_position = ordinal;
    m_row.m_process_id = pfs->m_processlist_id;

    return 0;
  }

  return HA_ERR_RECORD_DELETED;
}

int table_session_connect::read_row_values(TABLE *table, unsigned char *buf,
                                           Field **fields, bool read_all) {
  Field *f;

  /* Set the null bits */
  assert(table->s->null_bytes == 1);
  buf[0] = 0;

  for (; (f = *fields); fields++) {
    if (read_all || bitmap_is_set(table->read_set, f->field_index())) {
      switch (f->field_index()) {
        case FO_PROCESS_ID:
          if (m_row.m_process_id != 0) {
            set_field_ulonglong(f, m_row.m_process_id);
          } else {
            f->set_null();
          }
          break;
        case FO_ATTR_NAME:
          set_field_varchar_utf8mb4(f, m_row.m_attr_name,
                                    m_row.m_attr_name_length);
          break;
        case FO_ATTR_VALUE:
          if (m_row.m_attr_value_length)
            set_field_varchar_utf8mb4(f, m_row.m_attr_value,
                                      m_row.m_attr_value_length);
          else {
            f->set_null();
          }
          break;
        case FO_ORDINAL_POSITION:
          set_field_ulong(f, m_row.m_ordinal_position);
          break;
        default:
          assert(false);
      }
    }
  }
  return 0;
}

bool table_session_connect::thread_fits(PFS_thread *) { return true; }